Rng
struct · Source line 39
pub struct Rng { state: wrapping[u64], inc: wrapping[u64], }A seeded PCG32 generator. Copy it and you copy the position in the stream — the value semantics are the whole point of a replayable generator.
wolf-std d58f9e1
· toolchain pin verified against wolf 0.2.41 (0261597).
Source signatures and documentation; execution support is stated in each module.
All modules · Plain text · Stability tier: core
std.rand — a deterministic, explicitly seeded pseudo-random generator. Stability tier: core.
There is no entropy in Phase A (D15: the runtime stays thin, and comptime refuses ambient randomness by design). Every generator here is constructed from a seed the caller supplies, and the same seed gives the same stream on every implementation and every OS — the stream is part of the observable contract and tests/rand/ pins it value by value. OS-sourced bytes now live separately in std.os.random, with platform admission and the language's provider-failure trap. That surface neither seeds nor changes this generator; no hidden default.
The generator is PCG32 (O'Neill's XSH-RR variant over a 64-bit LCG): 64 bits of state, a 63-bit odd stream increment, a 32-bit output. It is chosen over a bare xorshift for its output permutation — the raw LCG's high bits are good and its low bits are not, and PCG's xorshift-then-rotate mixes them — and over anything larger because 64 bits of state is what wrapping[u64] gives us with no bit-twiddling the checked tier cannot lower. It is NOT cryptographically secure and this header says so once, loudly: a caller needing OS-source unpredictability uses std.os.random's guarded interface, never this PCG stream.
**Rng is a struct with free functions, not methods.** The contract writes Rng.new(seed); an impl block's methods cannot be called across a module boundary at these pins (F-0029), so the module ships rand.new(seed) and rand.next_int(mut r) — the container-first, mode-honest shape §8 already mandates for every other std container. The methods land when cross-module dispatch does.
struct · Source line 39
pub struct Rng { state: wrapping[u64], inc: wrapping[u64], }A seeded PCG32 generator. Copy it and you copy the position in the stream — the value semantics are the whole point of a replayable generator.
fn · Source line 59
pub fn new(seed: int) -> RngA generator seeded with seed. Two generators built from the same seed produce identical streams, forever, on every implementation. Total: no error row, no traps — every int is a legal seed, including 0 and negative values.
The stream increment is fixed (PCG's default), so seed selects a position in one stream rather than a stream of its own; a new_stream(seed, stream) constructor is the nursery candidate that would change that.
var a = rand.new(42)
var b = rand.new(42)
rand.next_int(mut a) == rand.next_int(mut b)fn · Source line 101
pub fn next_int(mut r: Rng) -> intThe next 32 bits of the stream, as an int in [0, 4294967296). Never negative and never above 2^32 - 1: the generator's output word is 32 bits wide, and widening it to int here (rather than producing a full-range int) keeps below unbiased with one draw in the common case. Total: no error row, no traps.
var r = rand.new(42)
rand.next_int(mut r) >= 0
rand.next_int(mut r) < 4294967296fn · Source line 125
pub fn below(mut r: Rng, n: int) -> intA uniform draw from [0, n), without modulo bias — the high draws that would skew the residue are rejected and redrawn, so the number of draws is data-dependent but the distribution is exact. Traps assert when n <= 0 (an empty range has nothing to draw from — a caller contract violation, §2's trap rule) and when n exceeds 2^32, which this generator's output word cannot cover.
var r = rand.new(7)
rand.below(mut r, 10) < 10
rand.below(mut r, 10) >= 0
rand.below(mut r, 1) == 0fn · Source line 153
pub fn next_float(mut r: Rng) -> f64A uniform draw from [0.0, 1.0) with 52 bits of resolution: the draw fills a mantissa, so every representable multiple of 2^-52 in the interval is equally likely and 1.0 is never returned. Total: no error row, no traps.
var r = rand.new(7)
rand.next_float(mut r) < 1.0
rand.next_float(mut r) >= 0.0fn · Source line 175
pub fn next_bool(mut r: Rng) -> boolA fair coin, taken from the output word's top bit (the LCG's best bit after the output permutation). Total: no error row, no traps.
var r = rand.new(7)
var seen = 0
for _ in 0..8 { if rand.next_bool(mut r) { seen = seen + 1 } }
seen > 0
seen < 8fn · Source line 199
pub fn shuffle(mut r: Rng, mut xs: List[int])Shuffles xs into a uniformly random permutation (Fisher–Yates, drawing from the front). Total: no error row, no traps; an empty or one-element list is left alone.
Cost note, honestly stated: this is O(n^2) at these pins, not O(n), because std.list.swap rebuilds the list (index assignment executes on one implementation only — F-0011's surface question). The algorithm is the right one and the constant is the language's to fix.
var r = rand.new(3)
var xs = List[int]()
(mut xs).push(1)
(mut xs).push(2)
(mut xs).push(3)
rand.shuffle(mut r, mut xs)
xs.len == 3
xs[0] + xs[1] + xs[2] == 6fn · Source line 218
pub fn choose(mut r: Rng, xs: List[int]) -> int ! {none}A uniformly chosen element of xs, or none when xs is empty. No traps.
var r = rand.new(3)
var xs = List[int]()
(mut xs).push(9)
(rand.choose(mut r, xs) else 0) == 9No public declarations in this section.
Tags named by public signatures: none. Their conditions are documented on the functions that return them.
No public declarations in this section.