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.
#
randomBooleanrandomBoolean() => boolean
Returns true
or false
with probability of 0.5
.
randomBoolean(); // falserandomBoolean(); // truerandomBoolean(); // true
#
randomDecimalrandomDecimal(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
#
randomFromrandomFrom(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
#
randomIndexrandomIndex(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
#
randomIntegerrandomInteger(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