random
Random numbers from two sources: a fast predictable generator for simulations and games, and the operating system’s secure source for keys and tokens.
int, float and shuffle use the predictable generator (xoshiro256**). It starts from a random seed, so each run differs, unless you call seed. Anyone who sees a few outputs can predict the rest, so never use it for secrets. secure_bytes, secure_int and token read the operating system’s secure source and cannot be seeded.
Roll dice, pick an item, shuffle. With a fixed seed the run repeats exactly, which helps tests:
import random
random.seed(42)let mut rolls: Vec<Str> = []for _ in 0..5 { rolls.push(random.int(1, 7).to_str())}print(rolls.join(" "))let colors = ["red", "green", "blue"]let mut deck = ["a", "b", "c", "d", "e"]random.shuffle(&mut deck)print(deck.join(""))print(colors[random.int(0, colors.len())])1 1 6 6 5bdaceblueMake a session id or a reset token. token(n) gives n secure bytes as URL-safe text; 16 bytes or more is enough to be unguessable:
import random
let id = random.token(16)print(id.len())22A secure number in a range, for example a 6-digit code:
import random
let code = random.secure_int(0, 1000000)let text = f"{code:06}"print(text.len())6Reference
Section titled “Reference”import randomFunctions
Section titled “Functions”pub fn seed(value: i64)Each thread’s generator starts from a random seed; seed fixes this thread’s, to repeat a run.
The generator state belongs to the thread. seed in the main program does not affect code running inside thread.join, thread.each or thread.workers; each new thread seeds itself from the operating system on first use, so no two threads share a sequence. Seed inside the thread if a threaded run must repeat.
pub fn int(low: i64, high: i64) -> i64A whole number in low..high (high excluded); predictable, so never for secrets.
high is excluded, so int(1, 7) is a die roll and int(0, v.len()) is a valid index. high must be greater than low; otherwise the program panics. Every value in the range is equally likely (no modulo bias).
pub fn float() -> f64A number in 0.0..1.0 (1.0 excluded); predictable, so never for secrets.
Gives a value in 0.0..1.0 with 53 bits of randomness. For a float in another range, scale it: low + random.float() * (high - low).
secure_bytes
Section titled “secure_bytes”pub fn secure_bytes(count: i64) -> Strcount bytes from the operating system’s secure source, for keys, salts and nonces.
Returns raw bytes, not text. Turn them into printable text with encoding.hex or encoding.base64, or use token. count must not be negative. If the operating system cannot supply random bytes, the program panics rather than return weak ones.
secure_int
Section titled “secure_int”pub fn secure_int(low: i64, high: i64) -> i64A whole number in low..high (high excluded) from the secure source, without bias.
pub fn token(bytes: i64) -> Strbytes secure random bytes as URL-safe text (base64url, no padding); 16 or more for session ids.
shuffle
Section titled “shuffle”pub fn shuffle<T>(items: &mut Vec<T>)Puts the items in a random order, every order equally likely (Fisher-Yates).
Uses the predictable generator, so a shuffle is repeatable after seed but not suitable for anything that must be unpredictable (for example dealing cards in a game played for money).