Generate a random boolean
Return true or false with equal probability.
function randomBoolean() { return Math.random() < 0.5; }randomCollection · 17 utilities
Random values for games, sampling and fixtures — and, where the value is a secret, the Web Crypto API instead of Math.random.
Return true or false with equal probability.
function randomBoolean() { return Math.random() < 0.5; }randomScale Math.random into [min, max).
function randomFloat(min, max) { return Math.random() * (max - min) + min; }randomReturn a #rrggbb color from 24 random bits.
function randomColor() { return `#${Math.floor(Math.random() * 0x1000000).toString(16).padStart…randomReturn an integer from min to max, both included.
function randomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }randomReturn a random IPv4 address from the ranges reserved for documentation.
function randomDocumentationIp() { const networks = ['192.0.2', '198.51.100', '203.0.113']; ret…randomReturn 1 or -1 with equal probability.
function randomSign() { return Math.random() < 0.5 ? -1 : 1; }randomDraw characters from an alphabet with a cryptographic generator, without bias.
function randomString(length, alphabet) { if (alphabet.length < 2 || alphabet.length > 256) thr…randomReturn random bytes from node:crypto as a hex string.
function randomToken(bytes = 32) { return randomBytes(bytes).toString('hex'); }randomReturn a lowercase Base32 identifier from a cryptographic generator.
function randomId(length) { const alphabet = '0123456789abcdefghjkmnpqrstvwxyz'; return Array.f…randomReturn a version 4 UUID from the platform’s cryptographic generator.
function uuid() { return crypto.randomUUID(); }randomReturn an integer in [min, max) skewed toward min.
function weightedRandom(min, max) { return Math.floor(Math.random() ** 2 * (max - min)) + min; …randomReturn count integers from min to max, both included.
function randomIntegers(min, max, count) { return Array.from({ length: count }, () => Math.floo…randomRemove one random item from the array and return it.
function takeRandomItem(values) { return values.splice(Math.floor(Math.random() * values.length…randomReturn one item at a random index.
function randomItem(values) { return values[Math.floor(Math.random() * values.length)]; }randomReturn count distinct items in random order.
function randomItems(values, count) { const pool = [...values]; for (let i = pool.length - 1; i…randomReturn one of the object’s own enumerable keys.
function randomKey(object) { const keys = Object.keys(object); return keys[Math.floor(Math.rand…randomSplit text into lines and return count of them in random order.
function randomLines(text, count) { const lines = text.split(/\r?\n/); for (let i = lines.lengt…randomThese functions take callbacks, the DOM or the runtime, so each page shows usage that the tests execute. Read the limits before using one in an application.