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.
§Panics
Panics if RNG state is currently borrowed by an active sampler callback.
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. The RNG is created lazily from
the current base seed on first use. The base seed is 0 unless it has been set with
ContextRandomExt::init_random.
§Panics
Panics if RNG state is already borrowed, such as when a sampler callback recursively samples from the same context.
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. The RNG is created lazily from the current base seed
on first use. The base seed is 0 unless it has been set with
ContextRandomExt::init_random.
§Panics
Panics if RNG state is already borrowed by an active sampler callback.
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,
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 RNG state is already borrowed or if the provided probability p
is not in the interval 0.0..=1.0.
Sourcefn sample_weighted<R: RngId + 'static, T>(
&self,
_rng_id: R,
weights: &[T],
) -> usize
fn sample_weighted<R: RngId + 'static, T>( &self, _rng_id: R, weights: &[T], ) -> usize
Draws a random entry out of the list provided in weights
with the given weights using the generator associated with the
given RngId. The weights must be nonnegative numbers with at
least one positive weight.
§Panics
Panics if RNG state is already borrowed or if weights is not a list
of nonnegative numbers with at least one positive weight.
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.