Skip to main content

Random Utils

RandomUtil wraps random number generation and selection operations. This utility class offers static methods to generate random values in various formats and ranges.

randomValue

Returns a random value from the specified array.

static randomValue<Type>(source:Type[]):Type
const fruits = [ 'apple', 'banana', 'orange', 'grape', 'kiwi' ];
const randomFruit = RandomUtil.randomValue(fruits);
console.log(randomFruit);
// Output: A random fruit from the array

const numbers = [ 10, 20, 30, 40, 50 ];
const randomNumber = RandomUtil.randomValue(numbers);
console.log(randomNumber);
// Output: A random number from the array

randomInt

Returns an integer in the [offset, offset + value) interval.

static randomInt(value:number, offset:number = 0):number
const randomValue = RandomUtil.randomInt(10);
console.log(randomValue);
// Output: A random integer from 0 to 9

const randomValue = RandomUtil.randomInt(10, 5);
console.log(randomValue);
// Output: A random integer from 5 to 14

randomBetweenInt

Returns a random integer in the [minValue, maxValue) interval.

static randomBetweenInt(minValue:number, maxValue:number):number
const randomValue = RandomUtil.randomBetweenInt(5, 10);
console.log(randomValue);
// Output: A random integer from 5 to 9

const enum SymbolType {
LP1 = 2000,
LP2 = 2001,
LP3 = 2002,
LP4 = 2003,
LP5 = 2004
}

const randomSymbol = RandomUtil.randomBetweenInt(SymbolType.LP1, SymbolType.LP5 + 1);
console.log(randomSymbol);
// Output: A random symbol from 2000 to 2004

randomSignInt

Returns a random integer value with a random sign in the [-offset - value, -offset) or [offset, offset + value) interval.

static randomSignInt(value:number, offset:number = 0):number
const randomValue = RandomUtil.randomSignInt(10);
console.log(randomValue);
// Output: A random integer from -10 to -1 or from 0 to 9

const randomValue = RandomUtil.randomSignInt(10, 5);
console.log(randomValue);
// Output: A random integer from -15 to -6 or from 5 to 14

random

Returns a floating-point value in the [offset, offset + value) interval.

static random(value:number, offset:number = 0):number
const randomValue = RandomUtil.random(1);
console.log(randomValue);
// Output: A random float from 0 to 1

const randomValue = RandomUtil.random(10, 5);
console.log(randomValue);
// Output: A random float from 5 to 15

randomBetween

Returns a floating-point value in the [minValue, maxValue) interval.

static randomBetween(minValue:number, maxValue:number):number
const randomValue = RandomUtil.randomBetween(5, 10);
console.log(randomValue);
// Output: A random float from 5 to 10

const randomValue = RandomUtil.randomBetween(0.25, 0.75);
console.log(randomValue);
// Output: A random float from 0.25 to 0.75

randomSign

Returns a random floating-point value with a random sign in the [-offset - value, -offset) or [offset, offset + value) interval.

static randomSignInt(value:number, offset:number = 0):number
const randomValue = RandomUtil.randomSign(10);
console.log(randomValue);
// Output: A random float from -10 to 0 or from 0 to 10

const randomValue = RandomUtil.randomSign(10, 5);
console.log(randomValue);
// Output: A random float from -15 to -5 or from 5 to 15

randomChance

Returns a boolean using the provided ratio.

static randomChance(numerator:number = 1, denominator:number = 2):boolean
const randomChance = RandomUtil.randomChance();
console.log(randomChance);
// Output: Either true or false with equal probability

const randomChance = RandomUtil.randomChance(1, 4);
console.log(randomChance);
// Output: true with 25% probability, false with 75% probability

randomColor

Returns a random RGB color.

static randomColor():number
const backgroundColor = RandomUtil.randomColor();
console.log(backgroundColor);
// Output: A random RGB color, e.g., 0x789ABC