cartulary 0.3.0-alpha.1

The knowledge layer of your project — decisions, issues, docs, all in one place.
Documentation
//! PRNG-backed [`IndexSampler`] used by the CLI forecast commands.
//!
//! The domain only sees the [`IndexSampler`] trait; `rand` lives here so
//! the choice of algorithm (and the entropy source for the seed) stays an
//! infra concern.

use rand::rngs::StdRng;
use rand::{Rng as _, SeedableRng as _};

use crate::domain::usecases::issue::IndexSampler;

/// Seeded [`StdRng`]-backed sampler. Construct it with a concrete `seed`
/// (the CLI resolves `--seed` or draws one from the OS).
pub struct SeededIndexSampler {
    rng: StdRng,
}

impl SeededIndexSampler {
    pub fn from_seed(seed: u64) -> Self {
        Self {
            rng: StdRng::seed_from_u64(seed),
        }
    }
}

impl IndexSampler for SeededIndexSampler {
    fn next_index(&mut self, len: usize) -> usize {
        self.rng.gen_range(0..len)
    }
}