Utilities for generating random results. Includes utilities to generate random numbers, integers in range, and randomizing in Arrays.
const reallyRandom = require('really-random');
Generate a random number between 0 and 1. This is an alias for Math.random()
.
const n = reallyRandom.random();
Generate a random Int in range of min to max - 1. randomRange(min, max)
.
const dieRoll = reallyRandom.randomRange(1, 6);
Generate a random Boolean.
const maybe = reallyRandom.randomBool();
Generate a die roll function. Returns a function that generates die rolls of an n sided die with numbers in the range of 1 to n.
const d6 = dieX(6);
console.log(d6() + d6()); // Outputs 2 to 12
const d20 = dieX(20);
const toHit = d20() + 3;
You can also pass the bonus into the die roll function.
const d6 = dieX(6);
console.log(d6(1)); // Outputs 2 to 7 simulates d6+1
const d20 = dieX(20);
const toHit = d20(3); // Outputs 4 to 23 simulates d20+3
Return a copy of an array with elements randomized.
const cards = ['a', 'b', 'c', 'd'];
const shuffled = reallyRandom.randomArray(cards);
Randomize an array in place.
const cards = ['a', 'b', 'c', 'd'];
const shuffledCards = reallyRandom.shuffleArray(cards);
Run Tests
- npm run test