pub struct RewardTable<const N: usize> { /* private fields */ }Implementations§
Source§impl<const N: usize> RewardTable<N>
impl<const N: usize> RewardTable<N>
pub fn new(weights: [u32; N]) -> Result<RewardTable<N>, LoopError>
Sourcepub fn choose(&self, ticket: u32) -> usize
pub fn choose(&self, ticket: u32) -> usize
Caller can supply a saved/replayed ticket. Zero weight is never selected.
Sourcepub fn roll(&self, rng: &mut RewardRng) -> usize
pub fn roll(&self, rng: &mut RewardRng) -> usize
Fixed work; multiply-high maps the full 64-bit sample to the weight range. This legacy mapping has rounding bias for totals that do not divide 2^64; its results and one-output RNG advancement are preserved.
Sourcepub fn roll_distinct_into(
&self,
rng: &mut RewardRng,
requested: usize,
out: &mut [usize],
sample_budget: usize,
) -> Result<DistinctDrawReport, DistinctDrawError>
pub fn roll_distinct_into( &self, rng: &mut RewardRng, requested: usize, out: &mut [usize], sample_budget: usize, ) -> Result<DistinctDrawReport, DistinctDrawError>
Weighted sampling without replacement, into caller storage.
For each attempt, consume exactly one RewardRng::next_u64 output and
use its upper 32 bits as x. For remaining total W, reject
x < (2^32 mod W); otherwise use x % W as a cumulative-weight ticket,
in original index order. Remove the selected index’s entire weight before
the next draw. Zero-weight entries never appear; no duplicate retries occur.
Even W == 1 consumes a sample for an accepted draw.
With independent uniform 32-bit samples, rejection leaves equally many
representatives of every ticket. Conditional on a next draw succeeding,
its index probability is weight[index] / W. A finite budget can censor
the prefix depending on earlier choices, so conditioning on an entire
request completing need not retain that distribution. SplitMix64 is a
deterministic gameplay PRNG, not a claim of independent random entropy or
security. There is no biased fallback on budget exhaustion.
Preflight checks, in order: requested <= MAX_DISTINCT_DRAWS, output has
at least min(requested, positive_weight_entries) slots, then
sample_budget <= MAX_DISTINCT_SAMPLES. Errors consume no RNG and write
nothing. Zero budget is valid: a nonzero request returns an empty prefix
with SampleBudgetExhausted; a zero request completes without samples.
Accepted or rejected attempts each consume one budget unit and RNG output.
Only the accepted prefix is written, including on exhaustion; no rollback.
At most N <= 256 scratch weights (1024 bytes), requested <= 256 writes,
and sample_budget <= 4096 samples. Work is O(N + sample_budget + N * min(requested, positive_weight_entries)), without allocation, floating
point, callbacks, wall clocks or authored-weight mutation. A fresh call
restarts the pool; do not concatenate partial calls as a continuation.
use ferravex_play::gameplay::loot::{RewardRng, RewardTable, DistinctDrawStatus};
let table = RewardTable::new([1, 0, 3]).unwrap();
let mut rng = RewardRng::new(0);
let mut out = [99; 3];
let report = table.roll_distinct_into(&mut rng, 3, &mut out, 8).unwrap();
assert_eq!(report.status, DistinctDrawStatus::TableExhausted);
assert_eq!((report.written, report.samples_used), (2, 2));
assert!(out[..2].contains(&0) && out[..2].contains(&2));
assert_eq!(out[2], 99);Sourcepub fn choose_distinct_into(
&self,
tickets: &[u32],
requested: usize,
out: &mut [usize],
) -> Result<DistinctDrawReport, DistinctDrawError>
pub fn choose_distinct_into( &self, tickets: &[u32], requested: usize, out: &mut [usize], ) -> Result<DistinctDrawReport, DistinctDrawError>
Deterministic distinct draws from saved/authored tickets, without RNG.
Apply each consumed ticket modulo the remaining weight total and
remove that selected index. No tickets are read after completion or pool
exhaustion. samples_used reports the number consumed, at most 256;
TicketsExhausted preserves the accepted prefix if the slice ends early.
This is an explicit replay mapping, not an unbiased conversion of arbitrary
random u32s. Independent uniform tickets in each remaining range give
sequential weight-proportional draws. Tickets depend on weights and order;
saved indices still depend on order. Hosts own stable reward identities and
any replay/content versioning.
Request/capacity preflight, tail preservation and fresh-pool semantics
match Self::roll_distinct_into. Work is O(N * (1 + min(requested, positive_weight_entries, tickets.len()))); storage is N scratch weights,
with no allocation. Even an arbitrarily long ticket slice is not scanned.
use ferravex_play::gameplay::loot::{RewardTable, DistinctDrawStatus};
let table = RewardTable::new([1, 0, 3]).unwrap();
let mut out = [99; 4];
let report = table.choose_distinct_into(&[3, 0, 7], 3, &mut out).unwrap();
assert_eq!(&out, &[2, 0, 99, 99]);
assert_eq!((report.written, report.samples_used), (2, 2));
assert_eq!(report.status, DistinctDrawStatus::TableExhausted);Trait Implementations§
Source§impl<const N: usize> Clone for RewardTable<N>
impl<const N: usize> Clone for RewardTable<N>
Source§fn clone(&self) -> RewardTable<N>
fn clone(&self) -> RewardTable<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more