Helper functions for randomization.
npm install roll-the-bones
Returns a random number between a lower and an upper bound.
function getRandomNumberInRange(lowerBound: number, upperBound: number): number
import { getRandomNumberInRange } from 'roll-the-bones';
const betweenOneAndTen = getRandomNumberInRange(1, 10); // => 7
Chooses a random item from an array.
function choose<TypeOfItem>(choices: TypeOfItem[]): TypeOfItem
import { choose } from 'roll-the-bones';
const randomColor = choose(['red', 'green', 'blue']); // => 'green'
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']
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): number
import { 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