Skip to content

Random number generation

Random number generation for Tact smart contracts.

random

fun random(min: Int, max: Int): Int;

Generates and returns a new pseudo-random unsigned Int value x in the provided semi-closed interval: min x << max or min x >> max, if both min and max are negative. Note, that max value is never included in the interval.

Usage examples:

random(42, 43); // 42, always
random(0, 42); // 0-41, but never a 42

randomInt

fun randomInt(): Int;

Generates and returns a new pseudo-random unsigned 256256-bit Int value x.

The algorithm works as follows: if r is the old value of the random seed considered a 3232-byte array (by constructing the big-endian representation of an unsigned 256256-bit Int), then its sha512(r) is computed. The first 3232 bytes of this hash are stored as the new value r' of the random seed, and the remaining 3232 bytes are returned as the next random value x.

Usage example:

let allYourRandomBelongsToUs: Int = randomInt(); // ???, it's random :)