Random number generation.
Import from the core package to use this module.
motoko name=import
import Random "mo:core/Random";public func blob() : async Blob@deprecated M0235
public func emptyState() : StateInitializes a random number generator state. This is used
to create a Random or AsyncRandom instance with a specific state.
The state is empty, but it can be reused after upgrading the canister.
Example:
import Random "mo:core/Random";
persistent actor {
let state = Random.emptyState();
transient let random = Random.cryptoFromState(state);
public func main() : async () {
let coin = await* random.bool(); // true or false
}
}@deprecated M0235
public func seedState(seed : Nat64) : SeedStateInitializes a pseudo-random number generator state with a 64-bit seed.
This is used to create a Random instance with a specific seed.
The seed is used to initialize the PRNG state.
Example:
import Random "mo:core/Random";
persistent actor {
let state = Random.seedState(123);
transient let random = Random.seedFromState(state);
public func main() : async () {
let coin = random.bool(); // true or false
}
}@deprecated M0235
public func seed(seed : Nat64) : RandomCreates a pseudo-random number generator from a 64-bit seed. The seed is used to initialize the PRNG state. This is suitable for simulations and testing, but not for cryptographic purposes.
Example:
motoko include=import
let random = Random.seed(123);
let coin = random.bool(); // true or false@deprecated M0235
public func seedFromState(state : SeedState) : RandomCreates a pseudo-random number generator with the given state. This provides statistical randomness suitable for simulations and testing, but should not be used for cryptographic purposes.
Example:
import Random "mo:core/Random";
persistent actor {
let state = Random.seedState(123);
transient let random = Random.seedFromState(state);
public func main() : async () {
let coin = random.bool(); // true or false
}
}@deprecated M0235
public func crypto() : AsyncRandomInitializes a cryptographic random number generator using entropy from the ICP management canister.
Example:
import Random "mo:core/Random";
persistent actor {
transient let random = Random.crypto();
public func main() : async () {
let coin = await* random.bool(); // true or false
}
}public func cryptoFromState(state : State) : AsyncRandomCreates a random number generator suitable for cryptography using entropy from the ICP management canister. Initializing from a state makes it possible to reuse entropy after upgrading the canister.
Example:
import Random "mo:core/Random";
persistent actor {
let state = Random.emptyState();
transient let random = Random.cryptoFromState(state);
func example() : async () {
let coin = await* random.bool(); // true or false
}
}public func bool() : BoolRandom choice between true and false.
Example:
motoko include=import
let random = Random.seed(42);
let coin = random.bool(); // true or false@deprecated M0235
public func nat8() : Nat8Random Nat8 value in the range [0, 256).
Example:
motoko include=import
let random = Random.seed(42);
let byte = random.nat8(); // 0 to 255@deprecated M0235
public func nat64() : Nat64Random Nat64 value in the range [0, 2^64).
Example:
motoko include=import
let random = Random.seed(42);
let number = random.nat64(); // 0 to 18446744073709551615@deprecated M0235
public func nat64Range(fromInclusive : Nat64, toExclusive : Nat64) : Nat64Random Nat64 value in the range [fromInclusive, toExclusive).
Example:
motoko include=import
let random = Random.seed(42);
let dice = random.nat64Range(1, 7); // 1 to 6@deprecated M0235
public func natRange(fromInclusive : Nat, toExclusive : Nat) : NatRandom Nat value in the range [fromInclusive, toExclusive).
Example:
motoko include=import
let random = Random.seed(42);
let index = random.natRange(0, 10); // 0 to 9@deprecated M0235
public func intRange(fromInclusive : Int, toExclusive : Int) : Int@deprecated M0235
public func bool() : async* BoolRandom choice between true and false.
public func nat8() : async* Nat8Random Nat8 value in the range [0, 256).
public func nat64() : async* Nat64Random Nat64 value in the range [0, 2^64).
public func nat64Range(fromInclusive : Nat64, toExclusive : Nat64) : async* Nat64Random Nat64 value in the range [fromInclusive, toExclusive).
public func natRange(fromInclusive : Nat, toExclusive : Nat) : async* NatRandom Nat value in the range [fromInclusive, toExclusive).
public func intRange(fromInclusive : Int, toExclusive : Int) : async* IntRandom Int value in the range [fromInclusive, toExclusive).