Module util

Source
Expand description

(build_module / register_array_extensions) is gone — there is no engine to register into. What remains here are the pure, typed versions of the random helpers the util module used to expose to scripts: rand_round, get_random_element, rand_weight, and drain_random.

count and index/weight arithmetic) but operate on native typed collections script ports call the typed equivalents directly: rand_round delegates to crate::mechanics::balance::rand_round_f64, and the drain/weight draws are reproduced by opponent_generation, loop_tasks, and currencies so their RNG sequences stay identical.

Functions§

drain_random
Remove and return a uniformly-random element from vec, returning None floor(random_f64() * len), clamped, then remove(idx).
get_random_element
Pick a uniformly-random element from slice, returning None for an empty floor(random_f64() * len), clamped to len - 1.
rand_round
Stochastic round of a non-integer value. Delegates to the shared native helper so every rand_round caller draws RNG identically.
rand_weight
rand_weight: sum the weights, draw random_f64() * sum_weight once, then walk the slice subtracting each weight until it goes non-positive. Returns None when the slice is empty or all weights sum to <= 0.0 (matching the old Dynamic::UNIT return). The last element is returned as the fallback if floating-point drift leaves the accumulator positive at the end.