roll-the-bones
v1.3.0
Published
Helper functions for randomization
Downloads
62
Readme
🎲 Roll the bones
Helper functions for randomization.
Installation
npm install roll-the-bonesUsage
getRandomNumberInRange
Returns a random number between a lower and an upper bound.
function getRandomNumberInRange(lowerBound: number, upperBound: number): numberimport { getRandomNumberInRange } from 'roll-the-bones';
const betweenOneAndTen = getRandomNumberInRange(1, 10); // => 7choose
Chooses a random item from an array.
function choose<TypeOfItem>(choices: TypeOfItem[]): TypeOfItemimport { choose } from 'roll-the-bones';
const randomColor = choose(['red', 'green', 'blue']); // => 'green'pick
Pick a few items from an array at random.
function pick<TypeOfItem>(items: TypeOfItem[], amount: number, putBack = false): TypeOfItem[]import { pick } from 'roll-the-bones';
const flavours = pick(['vanilla', 'chocolate', 'strawberry'], 2); // => ['strawberry', 'vanilla']By default an item is removed from the available options after being picked, but you can make sure it is put back and available to pick again if you set putBack to true.
const flavours = pick(['vanilla', 'chocolate', 'strawberry'], 3, true); // => ['chocolate', 'vanilla', 'chocolate']roll
Rolls one or more dice with a given amount of sides and returns the total. Amount of dice defaults to 1.
function roll(sides: number, amount?: number = 1): numberimport { roll } from 'roll-the-bones';
// The following rolls 2d6
const result = roll(6, 2); // => 9
const result = roll(6, 2); // => 3
const result = roll(6, 2); // => 12