Random Generator

Utility

Generate random strings, numbers, hex, alphanumeric, pick, shuffle, and more.

Import

Import
import { random } from 'toolmetry';

API Reference

FunctionParametersReturnsDescription
randomStringlength?: number, options?: objectstringGenerate a random string with options
randomIntmin: number, max: numbernumberRandom integer between min and max
randomHexlength?: numberstringGenerate a random hex string
randomAlphanumericlength?: numberstringGenerate a random alphanumeric string
randomPickarray: ArrayanyPick a random element from an array
randomShufflearray: ArrayArrayShuffle an array (Fisher-Yates)
randomBooleanbooleanGenerate a random boolean
randomFloatmin: number, max: number, decimals?: numbernumberRandom float between min and max

Examples

Random String
import { randomString } from 'toolmetry';

const str = randomString(16, { lowercase: true, uppercase: true, digits: true });
// "aB3xY9kL2mN5pQ8"

const simple = randomString(8, { digits: true, symbols: false });
// "aB3xY9kL"
Random Numbers
import { randomInt, randomFloat } from 'toolmetry';

const int = randomInt(1, 100); // e.g. 42
const float = randomFloat(0, 1, 4); // e.g. 0.5321
Hex & Alphanumeric
import { randomHex, randomAlphanumeric } from 'toolmetry';

const hex = randomHex(32);
// "a1b2c3d4e5f6..."

const alpha = randomAlphanumeric(16);
// "aB3xY9kL2mN5pQ8"
Pick & Shuffle
import { randomPick, randomShuffle } from 'toolmetry';

const picked = randomPick(['apple', 'banana', 'cherry']);
// "banana"

const shuffled = randomShuffle([1, 2, 3, 4, 5]);
// [3, 1, 5, 2, 4]

Try It Live