Module util

Source
Expand description

Weighted/uniform random draw helpers over [GameRng].

The implementations live in [event_system::rng] โ€” they are generic over GameRng and std collections with nothing game-specific in them. Re-exported here so the behaviors ports keep their mechanics::util:: paths, and so every caller draws RNG through the same index/weight arithmetic.

Functionsยง

drain_random
Remove and return a uniformly-random element of vec, None when empty. Same index formula as get_random_element, then Vec::remove โ€” so the remaining order is preserved and repeated draws sample without replacement.
get_random_element
Pick a uniformly-random element of slice, None when empty. Index is floor(random_f64() * len), clamped to len - 1 โ€” one f64 draw, matching GameRng::random_index.
rand_round
Stochastic round of a non-integer value: 2.3 yields 3 with probability 0.3 and 2 otherwise. Consumes one f64 draw when there is a fraction to resolve, none when value is already integral.
rand_weight
Weighted pick: sum the weights, draw random_f64() * sum once, then walk the slice subtracting each weight until the accumulator goes non-positive. None when the slice is empty or the weights sum to <= 0.0. The last element is the fallback if floating-point drift leaves the accumulator positive at the end.