Skip to content

infinity.random

Provides functionality for creating random strings and numbers. The random data is taken from the cryptographically secure random number generator of OpenSSL.

Module: infinity.crypto

Functions:

Example:

infinity.loadModule('infinity.crypto');

let randomDigits = infinity.random.digits();
console.info(randomDigits);
//0.6243089452413926

let randomCharacters = infinity.random.characters(10);
console.info(randomCharacters);
//6d9349f92c

Functions

digits()

Returns a cryptographically secure random number that is evenly distributed in the range 0 (inclusive) to 1 (exclusive), like Math.random() (53 random bits). Before 3.11 the result was 1 divided by a random signed 64-bit integer: a tiny number (around 1e-19), possibly negative and not evenly distributed.

Signature:

digits(): number

Return type: number

Example:

infinity.loadModule('infinity.crypto');

let randomDigits = infinity.random.digits();
console.info(randomDigits);
//0.6243089452413926

characters()

Returns a string of random lowercase hexadecimal characters (0-9, a-f) of the given length, i.e. length / 2 random bytes (rounded up) in hex encoding. Returns an empty string if length is less than 1 (before 3.11: less than 2).

Signature:

characters( length: number ): string

Parameters:

  • length: number

    The number of characters to return. Odd numbers are supported (before 3.11 the last character was not a random hex digit).

Return type: string

Example:

infinity.loadModule('infinity.crypto');

let randomCharacters = infinity.random.characters(10);
console.info(randomCharacters);
//6d9349f92c