1.0.12 • Published 6 years ago

random-weighted-pick v1.0.12

Weekly downloads
14
License
MIT
Repository
github
Last release
6 years ago

random-weighted-pick

A lightweight utility that allows you to easily choose a random item from a weighted list of items, with a probability dependent on their weight.

this module has no external dependencies and is licensed under the MIT License.

npm.io

npm.io codebeat badge

Installation & Usage

install

npm i -S random-weighted-pick

usage

import weightedPick from 'random-weighted-pick'
// OR
var weightedPick = require('random-weighted-pick');
const options = [
    { id: 0, weight: 0.2, item: () => console.log('Lemon') },
    { id: 1, weight: 0.3, item: ['Grape', 'Orange', 'Apple'] },
    { id: 2, weight: 0.4, item: 'Mango' },
    { id: 3, weight: 0.1, item: 3 }
]
// Sum of 'weights' should be equal 1.

const result = weightedPick(options)

console.log(result) // { id: 2, item: 'Mango' }

🔄 Run this example on Codepen

Development

install

npm install

Dependency module install

build

npm run build

Do some magic with ES6 to create ES5 code.

test

npm run test

Run test with Jest + Chai

develop

npm run dev : Run develop server

You can see result in

List of available tasks

clean

rm -f dist/*

Delete existing dist files

License

MIT License

Copyright (c) 2016 KossHackaton OneTeam
1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago