Skip to main content

Random Utilities

Random utilities use Math.random() under the hood, so they shouldn't be used for security related tasks. Use Cryto API for that instead.

randomBoolean#

randomBoolean() => boolean

Returns true or false with probability of 0.5.

randomBoolean(); // falserandomBoolean(); // truerandomBoolean(); // true

randomDecimal#

randomDecimal(min, max) => number

Returns a random decimal number from min (included) to max (excluded) uniformly distributed.

randomDecimal(0, 10); // 3.5477013297040689randomDecimal(0, 10); // 0randomDecimal(0, 10); // 9.9999999999999999

randomFrom#

randomFrom(source) => item

Pick a random item from an array or a charecter from a string. Uniform distribution.

randomFrom([1, 2, 3]); // 3randomFrom([1, 2, 3]); // 1
randomFrom("asdf"); // arandomFrom("asdf"); // d

randomIndex#

randomIndex(source) => number

Pick a random index in an array or a string. Uniform distribution.

randomIndex([1, 2, 3]); // 2randomIndex([1, 2, 3]); // 1
randomIndex("asdf"); // 0randomIndex("asdf"); // 3

randomInteger#

randomInteger(min, max) => number

Returns a random integer number from min (included) to max (excluded) uniformly distributed.

randomInteger(0, 10); // 7randomInteger(0, 10); // 9randomInteger(0, 10); // 0