moonpool_core/random.rs
1//! Random number generation provider abstraction.
2//!
3//! This module provides a provider pattern for random number generation,
4//! consistent with other provider abstractions in the simulation framework
5//! like TimeProvider, NetworkProvider, and TaskProvider.
6
7use rand::distr::{Distribution, StandardUniform, uniform::SampleUniform};
8use std::ops::Range;
9
10/// Provider trait for random number generation.
11///
12/// This trait abstracts random number generation to enable both
13/// deterministic simulation randomness and real random numbers
14/// in a unified way. Implementations handle the source of randomness
15/// appropriate for their environment.
16pub trait RandomProvider: Clone {
17 /// Generate a random value of type T.
18 ///
19 /// The type T must implement the Standard distribution.
20 fn random<T>(&self) -> T
21 where
22 StandardUniform: Distribution<T>;
23
24 /// Generate a random value within a specified range.
25 ///
26 /// The range is exclusive of the upper bound (start..end).
27 fn random_range<T>(&self, range: Range<T>) -> T
28 where
29 T: SampleUniform + PartialOrd;
30
31 /// Generate a random f64 between 0.0 and 1.0.
32 ///
33 /// This is a convenience method for generating ratios and percentages.
34 fn random_ratio(&self) -> f64;
35
36 /// Generate a random bool with the given probability of being true.
37 ///
38 /// The probability should be between 0.0 and 1.0.
39 fn random_bool(&self, probability: f64) -> bool;
40}