Module uniform
A distribution uniformly sampling numbers within a given range.
Uniform is the standard distribution to sample uniformly from a range;
e.g. Uniform::new_inclusive(1, 6).unwrap() can sample integers from 1 to 6, like a
standard die. Rng::random_range is implemented over Uniform.
Example usage
use Rng;
use Uniform;
let mut rng = rng;
let side = new.unwrap;
// sample between 1 and 10 points
for _ in 0..rng.random_range
Extending Uniform to support a custom type
To extend Uniform to support your own types, write a back-end which
implements the UniformSampler trait, then implement the SampleUniform
helper trait to "register" your back-end. See the MyF32 example below.
At a minimum, the back-end needs to store any parameters needed for sampling
(e.g. the target range) and implement new, new_inclusive and sample.
Those methods should include an assertion to check the range is valid (i.e.
low < high). The example below merely wraps another back-end.
The new, new_inclusive, sample_single and sample_single_inclusive
functions use arguments of
type SampleBorrow<X> to support passing in values by reference or
by value. In the implementation of these functions, you can choose to
simply use the reference returned by SampleBorrow::borrow, or you can choose
to copy or clone the value, whatever is appropriate for your type.
use *;
use ;
;
;
let = ;
let uniform = new.unwrap;
let x = uniform.sample;
Structs
- Uniform Sample values uniformly between two bounds.
Enums
-
Error
Error type returned from
Uniform::newandnew_inclusive.
Traits
-
SampleBorrow
Helper trait similar to
Borrowbut implemented only forSampleUniformand references toSampleUniformin order to resolve ambiguity issues. - SampleRange Range that supports generating a single sample efficiently.
-
SampleUniform
Helper trait for creating objects using the correct implementation of
UniformSamplerfor the sampling type. - UniformSampler Helper trait handling actual uniform sampling.