/randy

Put anything into a hat and pick them out in a variety of ways!

Primary LanguageJavaScript

randy

Randy lets you put things into a hat and pick them out in a variety of ways, including randomly.

Build Status Coverage Status

Installation

npm i @jag82/randy

Quick Start

Select random items...

const randy = new Randy([ 2, 1, 3]);
randy.get();  // returns 1, 2, or 3

...add more items on the fly...

randy.add('four');
randy.get();  // returns 1, 2, 3, or 'four'

...change selection options too!

randy.set({
    repeats: false
});

for(let i = 0; i < 100; i++) {
    randy.get();  // will not return the same result twice in a row, no matter how many times we call it!
}

Detailed Usage

Instantiation

...with an empty constructor:

const r = new Randy();
// r.items = [], default options

...with an array of items:

const r = new Randy([ 1, 2, 'three', { key: 'value' }]);

...with options:

//default options
const options = {
    repeats: true,  // can we repeat an item twice in a row?
    remove: false,   // remove items after selection?
    selection: 'random'  // random, fifo, lifo
};

const r = new Randy(options);

// or with an initial items array...
const r2 = new Randy([1, 2, 3], options);

Dynamically add/remove items:

const r = new Randy();

r.add(2);
r.add(['three', 'four']);
r.add(5, 6);
r.add({ seven: 7 });

r.remove(2);
r.remove({ seven: 7 });

// r.items = [ ['three', 'four'], 5, 6 ]

Dynamically update options:

const r = new Randy([1, 2, 3]);
r.get();
// r.items.length = 3

r.set({ 
    remove: true
});
r.get();
// r.items.length = 2

Statistics:

const r = new Randy([20, 30, 10]);
r.frequency();
r.reset();

TODO:

statistics

frequency

rename repo (hat?), will affect travis-ci, coveralls

publish to npm

integrate to piano tool