Module random

Expand description

See random for the system contract and examples. Reproducible SplitMix64 streams for engine variation, never cryptography. Forks depend on the original seed and stream ID, independent of parent draws.

Give each subsystem a stable name and keep its child stream between updates:

use ferravex_engine::random::Random;

let root = Random::new(42);
let mut loot = root.fork_named("loot");
let mut enemy = root.fork_tagged("enemies", 7); // Stable entity ID.
let coins = loot.try_range_u64(2..6).unwrap();
let offset = enemy.try_range_i64(-3..4).unwrap();
let speed = enemy.try_range_f32(1.0..2.0).unwrap();
assert!((2..6).contains(&coins));
assert!((-3..4).contains(&offset));
assert!((1.0..2.0).contains(&speed));

Integer range sampling is allocation-free. The half-open methods exclude the upper bound; Random::try_range_u64_inclusive and Random::try_range_i64_inclusive also support the maximum integer and the entire 64-bit domain. Invalid ranges consume no draws. Valid singletons and full-width ranges consume exactly one draw; other widths use the existing rejection sampler, whose draw count has no fixed cap.

§Seed and version contract

Random::VERSION is 1. The original seed, numeric Random::fork, SplitMix64 draw sequence, rejection sampling and 24-bit unit float mapping are unchanged. Named forks hash the exact UTF-8 bytes with 64-bit FNV-1a (offset 0xcbf29ce484222325, prime 0x100000001b3, wrapping arithmetic), then pass that hash to the original numeric fork. Tagged forks apply a numeric fork to the named child. No platform hasher, byte order, process state, allocation or parent draw count participates in seed derivation.

Names are case-sensitive and are not Unicode-normalized; an empty name is valid. Names are hashed to 64 bits, so distinct names can collide. Renaming a subsystem or changing its stable tag intentionally changes its sequence. Repeating a fork restarts that child; it does not resume previous draws. Hierarchical names use successive forks, not separator parsing: root.fork_named("world").fork_named("loot") differs from root.fork_named("world/loot"). Persist the version, seed and exact fork path when recording how to recreate a stream. No save format is introduced; Clone::clone provides an in-memory checkpoint of a stream’s current draws.

Structs§

Random
A seeded random stream with reproducible, independently derived children.