Skip to main content

@rtorcato/js-common / random

random

Functions

randomInt()

randomInt(min, max): number

Defined in: random/index.ts:14

Returns a random integer between min (inclusive) and max (inclusive).

Example

randomInt(1, 6) // 3 (any integer from 1 to 6)
randomInt(5, 5) // 5

Parameters

min

number

Minimum integer value.

max

number

Maximum integer value.

Returns

number

Random integer between min and max.


randomFloat()

randomFloat(min, max): number

Defined in: random/index.ts:31

Returns a random float between min (inclusive) and max (exclusive).

Example

randomFloat(0, 1) // 0.7342119...
randomFloat(10, 20) // 14.028...

Parameters

min

number

Minimum float value.

max

number

Maximum float value.

Returns

number

Random float between min and max.


randomBool()

randomBool(): boolean

Defined in: random/index.ts:46

Returns a random boolean value.

Example

randomBool() // true
randomBool() // false

Returns

boolean


randomElement()

randomElement<T>(arr): T | undefined

Defined in: random/index.ts:55

Returns a random element from an array.

Type Parameters

T

T

Parameters

arr

T[]

The array to pick from.

Returns

T | undefined

A random element from the array, or undefined if the array is empty.


randomString()

randomString(length, chars?): string

Defined in: random/index.ts:66

Returns a random string of the given length using the given characters.

Parameters

length

number

Length of the string.

chars?

string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

Characters to use (default: alphanumeric).

Returns

string