fastrand/core

sourcefastrand/core.sx

Rng

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

NameType
s0mut u64
s1mut u64
s2mut u64
s3mut u64

rotl

private
fn rotl(x: u64, n: u64) u64

mix64

private
fn mix64(x: u64) u64

new

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.

reseed

pub fn (Rng) reseed(rng: mut Rng, seed: u64) void

Re-seed an existing generator.

next_u64

pub fn (Rng) next_u64(rng: mut Rng) u64

Return the next 64 random bits.

next_u32

pub fn (Rng) next_u32(rng: mut Rng) u32

Return the next 32 random bits.

next_u16

pub fn (Rng) next_u16(rng: mut Rng) u16

Return the next 16 random bits.

next_u8

pub fn (Rng) next_u8(rng: mut Rng) u8

Return the next 8 random bits.

next_bool

pub fn (Rng) next_bool(rng: mut Rng) bool

Return a random boolean.

next_u64_below

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.

next_u32_below

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.

next_usize_below

pub fn (Rng) next_usize_below(rng: mut Rng, bound: usize) usize

Return a uniformly distributed usize in [0, bound).

next_range

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.

next_f64

pub fn (Rng) next_f64(rng: mut Rng) f64

Return a random f64 in [0.0, 1.0).

next_f32

pub fn (Rng) next_f32(rng: mut Rng) f32

Return a random f32 in [0.0, 1.0).

fill_bytes

pub fn (Rng) fill_bytes(rng: mut Rng, buf: ref u8, len: usize) void

Fill len bytes at buf with random data.

fill

pub fn (Rng) fill(rng: mut Rng, buf: ref u8, len: usize) void

Fill a byte buffer with random data.

choose

pub fn (Rng) choose(rng: mut Rng, n: usize) usize

Return a uniformly distributed index in [0, n).

shuffle

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).

seed

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.

random_u64

pub fn random_u64() u64

Return a fast random u64 from the module-level generator.

random_u32

pub fn random_u32() u32

Return a fast random u32 from the module-level generator.

random_bool

pub fn random_bool() bool

Return a fast random boolean from the module-level generator.

random_u64_below

pub fn random_u64_below(bound: u64) u64

Return a fast random u64 in [0, bound) from the module-level generator.

random_u32_below

pub fn random_u32_below(bound: u32) u32

Return a fast random u32 in [0, bound) from the module-level generator.

random_range

pub fn random_range(lo: i64, hi: i64) i64

Return a fast random i64 in [lo, hi) from the module-level generator.

random_f64

pub fn random_f64() f64

Return a fast random f64 in [0.0, 1.0) from the module-level generator.

random_f32

pub fn random_f32() f32

Return a fast random f32 in [0.0, 1.0) from the module-level generator.

fill

pub fn fill(buf: ref u8, len: usize) void

Fill a byte buffer from the module-level generator.

random_lowercase

pub fn random_lowercase() u8

Return a random lowercase ASCII letter ('a'..='z').

random_uppercase

pub fn random_uppercase() u8

Return a random uppercase ASCII letter ('A'..='Z').

random_alphabetic

pub fn random_alphabetic() u8

Return a random ASCII letter.

random_digit

pub fn random_digit() u8

Return a random ASCII digit ('0'..='9').

random_alphanumeric

pub fn random_alphanumeric() u8

Return a random ASCII letter or digit.