1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*!
Generating random samples from probability distributions.
This module is the home of the [`Distribution`] trait and several of its implementations.
It is the workhorse behind some of the convenient functionality of the [`Random`] struct,
e.g. [`Random::next`], [`Random::range`] and of course [`Random::sample`].
Abstractly, a [probability distribution] describes the probability of occurance of each value in its sample space.
More concretely, an implementation of `Distribution<T>` for type `D` is an algorithm for choosing values from the sample space (a subset of `T`)
according to the distribution `D` represents, using an external source of randomness (an Rng supplied to the `sample` function).
A type `D` may implement `Distribution<T>` for multiple types `T`.
Any type implementing [`Distribution`] is stateless (i.e. immutable), but it may have internal parameters set at construction time
(for example, [`Uniform`] allows specification of its sample space as a range within `T`).
# The `StandardUniform` distribution
The [`StandardUniform`] distribution is important to mention.
This is the distribution used by [`Random::next`] and represents the "default" way to produce a random value for many different types,
including most primitive types, tuples, arrays, and a few derived types. See the documentation of [`StandardUniform`] for more details.
Implementing `Distribution<T>` for [`StandardUniform`] for user types `T` makes it possible to generate type `T` with [`Random::next`].
# The `Uniform` distribution
The [`Uniform`] distribution is similar to the [`StandardUniform`] distribution
but it allows the sample space to be specified as an arbitrary range within its target type `T`.
Both [`StandardUniform`] and [`Uniform`] are in some sense uniform distributions.
Values may be sampled from this distribution using [`Random::range`] or by creating a distribution object from a `low..high` or `low..=high`.
When the range limits are not known at compile time it is typically faster to reuse an existing distribution object than to call [`Random::range`].
User types `T` may also implement `Distribution<T>` for [`Uniform`], although this is less straightforward than for [`StandardUniform`]
(see the documentation in the uniform module. Doing so enables generation of values of type `T` with [`Random::range`].
[probability distribution]: https://en.wikipedia.org/wiki/Probability_distribution
*/
use ;
use crate::;
pub use Alnum;
pub use Bernoulli;
pub use Dice;
pub use Float01;
pub use Samples;
pub use StandardUniform;
pub use *;
cfg_if!
/// Types (distributions) that can be used to create a random instance of `T`.
///
/// It is possible to sample from a distribution through both the
/// `Distribution` trait and [`Random`] struct, via `distr.sample(&mut rand)` and
/// `rand.sample(&distr)`. There's also the [`Random::samples`] method, which
/// produces an iterator that samples from the distribution.
///
/// All implementations are expected to be immutable; this has the significant advantage of not needing to consider thread safety,
/// and for most distributions efficient state-less sampling algorithms are available.
///
/// Implementations are typically expected to be portable with reproducible results when used with a PRNG with fixed seed;
/// see the [portability chapter] of The Rust Rand Book. In some cases this does not apply,
/// e.g. the `usize` type requires different sampling on 32-bit and 64-bit machines.
///
/// [portability chapter]: https://rust-random.github.io/book/portability.html
/// Distribution of values of type `U` derived from the distribution `D`.
///
/// This struct is created by the [`Distribution::map`] method.
/// See its documentation for more.