weight-randomizer
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

weight randomizer

creates random array sequence based on group weight and provided length of sequence. Implementation is based on fair die algorithm http://www.keithschwarz.com/darts-dice-coins/. Algorithm complexity is O(N) at setup time, and guaranteed O(1) run time for every random selection.

get started

  • run
npm i weight-randomizer
  • require into your project via
import WeightRandomizer from "./weight.randomizer";

how it works

Method getSequence accepts two arguments:

  • objectWeights: Array of objects and its weights. Objects are of any[] type.
  • sequenceLength: Number of elements in resulted array

Result of method execution will be array[sequenceLength]. Elements will be of type any. Distribution of elements in resulted array is based on groups weight. Every element inside group has same possibilty to be picked up.

Example is available in the src/weight.randomier.spec.ts test file.

Package Sidebar

Install

npm i weight-randomizer

Weekly Downloads

19

Version

1.0.4

License

ISC

Unpacked Size

12 kB

Total Files

6

Last publish

Collaborators

  • minjas