fastrand/core
sourcefastrand/core.sx
pub type Rng = { s0: mut u64, s1: mut u64, s2: mut u64, s3: mut u64 }
A fast xoshiro256++ generator.
The state is held by value so an Rng can live on the stack, inside another type, or in a global. Methods declared with mut self advance the state in place.
Fields
| Name | Type |
|---|
s0 | mut u64 |
s1 | mut u64 |
s2 | mut u64 |
s3 | mut u64 |
fn rotl(x: u64, n: u64) u64
pub fn (Rng) new(seed: u64) Rng
Create a generator from a 64-bit seed.
The same seed always produces the same sequence. This is useful for reproducible tests and simulations. The splitmix64 expansion makes even very small or very similar seeds produce well-separated states.
pub fn (Rng) reseed(rng: mut Rng, seed: u64) void
Re-seed an existing generator.
pub fn (Rng) next_u64(rng: mut Rng) u64
Return the next 64 random bits.
pub fn (Rng) next_u32(rng: mut Rng) u32
Return the next 32 random bits.
pub fn (Rng) next_u16(rng: mut Rng) u16
Return the next 16 random bits.
pub fn (Rng) next_u8(rng: mut Rng) u8
Return the next 8 random bits.
pub fn (Rng) next_bool(rng: mut Rng) bool
pub fn (Rng) next_u64_below(rng: mut Rng, bound: u64) u64
Return a uniformly distributed u64 in [0, bound).
bound must be greater than zero. Rejection sampling removes the modulo bias while keeping the common path fast.
pub fn (Rng) next_u32_below(rng: mut Rng, bound: u32) u32
Return a uniformly distributed u32 in [0, bound).
bound must be greater than zero.
pub fn (Rng) next_usize_below(rng: mut Rng, bound: usize) usize
Return a uniformly distributed usize in [0, bound).
pub fn (Rng) next_range(rng: mut Rng, lo: i64, hi: i64) i64
Return a uniformly distributed i64 in [lo, hi).
Requires lo < hi. The full i64 range is supported.
pub fn (Rng) next_f64(rng: mut Rng) f64
Return a random f64 in [0.0, 1.0).
pub fn (Rng) next_f32(rng: mut Rng) f32
Return a random f32 in [0.0, 1.0).
pub fn (Rng) fill_bytes(rng: mut Rng, buf: ref u8, len: usize) void
Fill len bytes at buf with random data.
pub fn (Rng) fill(rng: mut Rng, buf: ref u8, len: usize) void
Fill a byte buffer with random data.
pub fn (Rng) choose(rng: mut Rng, n: usize) usize
Return a uniformly distributed index in [0, n).
pub fn (Rng) shuffle[T](rng: mut Rng, items: mut list[T]) void
Shuffle a list in place using Fisher-Yates.
Call with an explicit element type: Rng.shuffle[i64](rng, items).
pub fn seed(s: u64) void
Seed the module-level generator.
The module-level functions (random_u64, random_range, ...) use this generator. Seeding it makes their output reproducible across runs.
pub fn random_u64() u64
Return a fast random u64 from the module-level generator.
pub fn random_u32() u32
Return a fast random u32 from the module-level generator.
pub fn random_bool() bool
Return a fast random boolean from the module-level generator.
pub fn random_u64_below(bound: u64) u64
Return a fast random u64 in [0, bound) from the module-level generator.
pub fn random_u32_below(bound: u32) u32
Return a fast random u32 in [0, bound) from the module-level generator.
pub fn random_range(lo: i64, hi: i64) i64
Return a fast random i64 in [lo, hi) from the module-level generator.
pub fn random_f64() f64
Return a fast random f64 in [0.0, 1.0) from the module-level generator.
pub fn random_f32() f32
Return a fast random f32 in [0.0, 1.0) from the module-level generator.
pub fn fill(buf: ref u8, len: usize) void
Fill a byte buffer from the module-level generator.
pub fn random_lowercase() u8
Return a random lowercase ASCII letter ('a'..='z').
pub fn random_uppercase() u8
Return a random uppercase ASCII letter ('A'..='Z').
pub fn random_alphabetic() u8
Return a random ASCII letter.
pub fn random_digit() u8
Return a random ASCII digit ('0'..='9').
pub fn random_alphanumeric() u8
Return a random ASCII letter or digit.