7
7
// option. This file may not be copied, modified, or distributed
8
8
// except according to those terms.
9
9
10
- //! Generating random samples from probability distributions.
10
+ //! Generating random samples from probability distributions
11
11
//!
12
12
//! This module is the home of the [`Distribution`] trait and several of its
13
13
//! implementations. It is the workhorse behind some of the convenient
14
- //! functionality of the [`Rng`] trait, including [`gen`], [`gen_range`] and
15
- //! of course [`sample`].
14
+ //! functionality of the [`Rng`] trait, e.g. [`Rng:: gen`], [`Rng:: gen_range`] and
15
+ //! of course [`Rng:: sample`].
16
16
//!
17
17
//! Abstractly, a [probability distribution] describes the probability of
18
18
//! occurance of each value in its sample space.
40
40
//! possible to generate type `T` with [`Rng::gen()`], and by extension also
41
41
//! with the [`random()`] function.
42
42
//!
43
+ //! ## Random characters
44
+ //!
45
+ //! [`Alphanumeric`] is a simple distribution to sample random letters and
46
+ //! numbers of the `char` type; in contrast [`Standard`] may sample any valid
47
+ //! `char`.
48
+ //!
43
49
//!
44
- //! # Distribution to sample from a ` Uniform` range
50
+ //! # Uniform numeric ranges
45
51
//!
46
52
//! The [`Uniform`] distribution is more flexible than [`Standard`], but also
47
53
//! more specialised: it supports fewer target types, but allows the sample
59
65
//! documentation in the [`uniform`] module. Doing so enables generation of
60
66
//! values of type `T` with [`Rng::gen_range`].
61
67
//!
62
- //!
63
- //! # Other distributions
68
+ //! ## Open and half-open ranges
64
69
//!
65
70
//! There are surprisingly many ways to uniformly generate random floats. A
66
71
//! range between 0 and 1 is standard, but the exact bounds (open vs closed)
67
72
//! and accuracy differ. In addition to the [`Standard`] distribution Rand offers
68
73
//! [`Open01`] and [`OpenClosed01`]. See "Floating point implementation" section of
69
74
//! [`Standard`] documentation for more details.
70
75
//!
71
- //! [`Alphanumeric`] is a simple distribution to sample random letters and
72
- //! numbers of the `char` type; in contrast [`Standard`] may sample any valid
73
- //! `char`.
74
- //!
75
- //! [`WeightedIndex`] can be used to do weighted sampling from a set of items,
76
- //! such as from an array.
77
- //!
78
- //! # Non-uniform probability distributions
79
- //!
80
- //! Rand currently provides the following probability distributions:
81
- //!
82
- //! - Related to real-valued quantities that grow linearly
83
- //! (e.g. errors, offsets):
84
- //! - [`Normal`] distribution, and [`StandardNormal`] as a primitive
85
- //! - [`Cauchy`] distribution
86
- //! - Related to Bernoulli trials (yes/no events, with a given probability):
87
- //! - [`Binomial`] distribution
88
- //! - [`Bernoulli`] distribution, similar to [`Rng::gen_bool`].
89
- //! - Related to positive real-valued quantities that grow exponentially
90
- //! (e.g. prices, incomes, populations):
91
- //! - [`LogNormal`] distribution
92
- //! - Related to the occurrence of independent events at a given rate:
93
- //! - [`Pareto`] distribution
94
- //! - [`Poisson`] distribution
95
- //! - [`Exp`]onential distribution, and [`Exp1`] as a primitive
96
- //! - [`Weibull`] distribution
97
- //! - Gamma and derived distributions:
98
- //! - [`Gamma`] distribution
99
- //! - [`ChiSquared`] distribution
100
- //! - [`StudentT`] distribution
101
- //! - [`FisherF`] distribution
102
- //! - Triangular distribution:
103
- //! - [`Beta`] distribution
104
- //! - [`Triangular`] distribution
105
- //! - Multivariate probability distributions
106
- //! - [`Dirichlet`] distribution
107
- //! - [`UnitSphereSurface`] distribution
108
- //! - [`UnitCircle`] distribution
109
- //!
110
- //! # Examples
76
+ //! # Non-uniform sampling
111
77
//!
112
- //! Sampling from a distribution:
78
+ //! Sampling a simple true/false outcome with a given probability has a name:
79
+ //! the [`Bernoulli`] distribution (this is used by [`Rng::gen_bool`]).
113
80
//!
114
- //! ```
115
- //! use rand::{thread_rng, Rng};
116
- //! use rand::distributions::Exp;
81
+ //! For weighted sampling from a sequence of discrete values, use the
82
+ //! [`weighted`] module.
117
83
//!
118
- //! let exp = Exp::new(2.0);
119
- //! let v = thread_rng().sample(exp);
120
- //! println!("{} is from an Exp(2) distribution", v);
121
- //! ```
122
- //!
123
- //! Implementing the [`Standard`] distribution for a user type:
124
- //!
125
- //! ```
126
- //! # #![allow(dead_code)]
127
- //! use rand::Rng;
128
- //! use rand::distributions::{Distribution, Standard};
129
- //!
130
- //! struct MyF32 {
131
- //! x: f32,
132
- //! }
133
- //!
134
- //! impl Distribution<MyF32> for Standard {
135
- //! fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MyF32 {
136
- //! MyF32 { x: rng.gen() }
137
- //! }
138
- //! }
139
- //! ```
84
+ //! This crate no longer includes other non-uniform distributions; instead
85
+ //! it is recommended that you use either [`rand_distr`] or [`statrs`].
140
86
//!
141
87
//!
142
88
//! [probability distribution]: https://en.wikipedia.org/wiki/Probability_distribution
143
- //! [`gen_range`]: Rng::gen_range
144
- //! [`gen`]: Rng::gen
145
- //! [`sample`]: Rng::sample
146
- //! [`new_inclusive`]: Uniform::new_inclusive
89
+ //! [`rand_distr`]: https://crates.io/crates/rand_distr
90
+ //! [`statrs`]: https://crates.io/crates/statrs
91
+
147
92
//! [`Alphanumeric`]: distributions::Alphanumeric
148
93
//! [`Bernoulli`]: distributions::Bernoulli
149
- //! [`Beta`]: distributions::Beta
150
- //! [`Binomial`]: distributions::Binomial
151
- //! [`Cauchy`]: distributions::Cauchy
152
- //! [`ChiSquared`]: distributions::ChiSquared
153
- //! [`Dirichlet`]: distributions::Dirichlet
154
- //! [`Exp`]: distributions::Exp
155
- //! [`Exp1`]: distributions::Exp1
156
- //! [`FisherF`]: distributions::FisherF
157
- //! [`Gamma`]: distributions::Gamma
158
- //! [`LogNormal`]: distributions::LogNormal
159
- //! [`Normal`]: distributions::Normal
160
94
//! [`Open01`]: distributions::Open01
161
95
//! [`OpenClosed01`]: distributions::OpenClosed01
162
- //! [`Pareto`]: distributions::Pareto
163
- //! [`Poisson`]: distributions::Poisson
164
96
//! [`Standard`]: distributions::Standard
165
- //! [`StandardNormal`]: distributions::StandardNormal
166
- //! [`StudentT`]: distributions::StudentT
167
- //! [`Triangular`]: distributions::Triangular
168
97
//! [`Uniform`]: distributions::Uniform
169
98
//! [`Uniform::new`]: distributions::Uniform::new
170
99
//! [`Uniform::new_inclusive`]: distributions::Uniform::new_inclusive
171
- //! [`UnitSphereSurface`]: distributions::UnitSphereSurface
172
- //! [`UnitCircle`]: distributions::UnitCircle
173
- //! [`Weibull`]: distributions::Weibull
174
- //! [`WeightedIndex`]: distributions::WeightedIndex
100
+ //! [`weighted`]: distributions::weighted
101
+ //! [`rand_distr`]: https://crates.io/crates/rand_distr
102
+ //! [`statrs`]: https://crates.io/crates/statrs
175
103
176
104
#[ cfg( any( rustc_1_26, features="nightly" ) ) ]
177
105
use core:: iter;
@@ -334,7 +262,7 @@ impl<'a, D, R, T> iter::TrustedLen for DistIter<'a, D, R, T>
334
262
/// Usually generates values with a numerically uniform distribution, and with a
335
263
/// range appropriate to the type.
336
264
///
337
- /// ## Built-in Implementations
265
+ /// ## Provided implementations
338
266
///
339
267
/// Assuming the provided `Rng` is well-behaved, these implementations
340
268
/// generate values with the following ranges and distributions:
@@ -359,7 +287,27 @@ impl<'a, D, R, T> iter::TrustedLen for DistIter<'a, D, R, T>
359
287
/// * `Option<T>` where `Standard` is implemented for `T`: Returns `None` with
360
288
/// probability 0.5; otherwise generates a random `x: T` and returns `Some(x)`.
361
289
///
362
- /// # Example
290
+ /// ## Custom implementations
291
+ ///
292
+ /// The [`Standard`] distribution may be implemented for user types as follows:
293
+ ///
294
+ /// ```
295
+ /// # #![allow(dead_code)]
296
+ /// use rand::Rng;
297
+ /// use rand::distributions::{Distribution, Standard};
298
+ ///
299
+ /// struct MyF32 {
300
+ /// x: f32,
301
+ /// }
302
+ ///
303
+ /// impl Distribution<MyF32> for Standard {
304
+ /// fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MyF32 {
305
+ /// MyF32 { x: rng.gen() }
306
+ /// }
307
+ /// }
308
+ /// ```
309
+ ///
310
+ /// ## Example usage
363
311
/// ```
364
312
/// use rand::prelude::*;
365
313
/// use rand::distributions::Standard;
0 commit comments