Skip to main content

Module rand

Module rand 

Source
Expand description

Canister-local deterministic PRNG seeded externally (e.g. via raw_rand).

The IC executes canister code single-threaded, so RefCell provides sufficient interior mutability without locking.

The RNG must be explicitly seeded before use (typically during init or post-upgrade) and is intended for update calls where state advancement is permitted.

Enums§

RngError
RngError Errors raised when randomness is unavailable.

Functions§

fill_bytes
Fill the provided buffer with random bytes.
is_seeded
Returns true if the RNG has been seeded.
next_u8
Produce an 8-bit random value (derived from next_u16).
next_u16
Produce a 16-bit random value from the shared RNG.
next_u32
Produce a 32-bit random value from the shared RNG.
next_u64
Produce a 64-bit random value from the shared RNG.
next_u128
Produce a 128-bit random value from the shared RNG.
random_bytes
Produce random bytes using the shared RNG.
random_hex
Produce hex-encoded random bytes using the shared RNG.
seed_from
Seed the RNG with a 32-byte value (e.g. management canister raw_rand output).