pub trait ContextRandomExt: ContextBase {
// Provided methods
fn init_random(&mut self, base_seed: u64) { ... }
fn sample<R: RngId + 'static, T>(
&self,
_rng_type: R,
sampler: impl FnOnce(&mut R::RngType) -> T,
) -> T { ... }
fn debug_rng_state<R: RngId + 'static>(&self, _rng_id: R) -> u64
where R::RngType: Clone + Rng { ... }
fn sample_distr<R: RngId + 'static, T>(
&self,
_rng_type: R,
distribution: impl Distribution<T>,
) -> T
where R::RngType: Rng { ... }
fn sample_range<R: RngId + 'static, S, T>(&self, rng_id: R, range: S) -> T
where R::RngType: Rng,
S: SampleRange<T>,
T: SampleUniform { ... }
fn sample_bool<R: RngId + 'static>(
&self,
rng_id: R,
p: impl Into<f64>,
) -> bool
where R::RngType: Rng { ... }
fn sample_weighted<R: RngId + 'static, T>(
&self,
_rng_id: R,
weights: &[T],
) -> usize
where R::RngType: Rng,
T: Clone + Default + SampleUniform + for<'a> AddAssign<&'a T> + PartialOrd + Weight { ... }
}Provided Methods§
Sourcefn init_random(&mut self, base_seed: u64)
fn init_random(&mut self, base_seed: u64)
Initializes the RngPlugin data container to store rngs as well as a base
seed. Note that rngs are created lazily when get_rng is called.
Sourcefn sample<R: RngId + 'static, T>(
&self,
_rng_type: R,
sampler: impl FnOnce(&mut R::RngType) -> T,
) -> T
fn sample<R: RngId + 'static, T>( &self, _rng_type: R, sampler: impl FnOnce(&mut R::RngType) -> T, ) -> T
Gets a random sample from the random number generator associated with the given
RngId by applying the specified sampler function. If the Rng has not been used
before, one will be created with the base seed you defined in set_base_random_seed.
Note that this will panic if set_base_random_seed was not called yet.
Sourcefn debug_rng_state<R: RngId + 'static>(&self, _rng_id: R) -> u64
fn debug_rng_state<R: RngId + 'static>(&self, _rng_id: R) -> u64
Returns a deterministic debug fingerprint for the current position of the random number
generator associated with the given RngId.
This clones the current generator and hashes a small number of draws from the clone, so it does not advance the generator used by sampling. The returned value is intended only for comparing checkpoints across runs of the same code and dependency versions. It is not a serialization format, and it does not guarantee reproducibility across RNG algorithm or version changes.
Sourcefn sample_distr<R: RngId + 'static, T>(
&self,
_rng_type: R,
distribution: impl Distribution<T>,
) -> Twhere
R::RngType: Rng,
fn sample_distr<R: RngId + 'static, T>(
&self,
_rng_type: R,
distribution: impl Distribution<T>,
) -> Twhere
R::RngType: Rng,
Gets a random sample from the specified distribution using a random number generator
associated with the given RngId. If the Rng has not been used before, one will be
created with the base seed you defined in set_base_random_seed.
Note that this will panic if set_base_random_seed was not called yet.
Sourcefn sample_range<R: RngId + 'static, S, T>(&self, rng_id: R, range: S) -> Twhere
R::RngType: Rng,
S: SampleRange<T>,
T: SampleUniform,
fn sample_range<R: RngId + 'static, S, T>(&self, rng_id: R, range: S) -> Twhere
R::RngType: Rng,
S: SampleRange<T>,
T: SampleUniform,
Gets a random sample within the range provided by range
using the generator associated with the given RngId.
Note that this will panic if set_base_random_seed was not called yet.
Sourcefn sample_bool<R: RngId + 'static>(&self, rng_id: R, p: impl Into<f64>) -> boolwhere
R::RngType: Rng,
fn sample_bool<R: RngId + 'static>(&self, rng_id: R, p: impl Into<f64>) -> boolwhere
R::RngType: Rng,
Gets a random boolean value which is true with probability p using the
generator associated with the given RngId. The supplied probability
is converted to f64 before sampling.
use ixa::{define_rng, Context, ContextRandomExt};
define_rng!(ExampleRng);
struct Probability(f64);
impl From<Probability> for f64 {
fn from(probability: Probability) -> Self {
probability.0
}
}
let mut context = Context::new();
context.init_random(42);
assert!(context.sample_bool(ExampleRng, Probability(1.0)));§Panics
Panics if the converted probability is outside [0.0, 1.0] or is NaN.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.