@@ -177,7 +177,7 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
177
177
/// use rand::distributions::Uniform;
178
178
///
179
179
/// let val: f32 = SmallRng::new().sample(Uniform);
180
- /// println!("f32 from [ 0,1): {}", val);
180
+ /// println!("f32 from ( 0,1): {}", val);
181
181
/// ```
182
182
///
183
183
/// With dynamic dispatch (type erasure of `Rng`):
@@ -189,9 +189,29 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
189
189
/// let mut rng = thread_rng();
190
190
/// let mut erased_rng: &mut RngCore = &mut rng;
191
191
/// let val: f32 = erased_rng.sample(Uniform);
192
- /// println!("f32 from [ 0,1): {}", val);
192
+ /// println!("f32 from ( 0,1): {}", val);
193
193
/// ```
194
194
///
195
+ /// # Open interval for floats
196
+ /// In theory it is possible to choose between an open interval `(0, 1)`, and
197
+ /// the half-open intervals `[0, 1)` and `(0, 1]`. All can give a distribution
198
+ /// with perfectly uniform intervals. Many libraries in other programming
199
+ /// languages default to the closed-open interval `[0, 1)`. We choose here to go
200
+ /// with *open*, with the arguments:
201
+ ///
202
+ /// - The chance to generate a specific value, like exactly 0.0, is *tiny*. No
203
+ /// (or almost no) sensible code relies on an exact floating-point value to be
204
+ /// generated with a very small chance (1 in ~8 million (2^23) for `f32`, and
205
+ /// 1 in 2^52 for `f64`). What is relied on is having a uniform distribution
206
+ /// and a mean of `0.5`.
207
+ /// - Several common algorithms rely on never seeing the value `0.0` generated,
208
+ /// i.e. they rely on an open interval. For example when the logarithm of the
209
+ /// value is taken, or used as a devisor.
210
+ ///
211
+ /// In other words, the guarantee some value *could* be generated is less useful
212
+ /// than the guarantee some value (`0.0`) is never generated. That makes an open
213
+ /// interval a nicer choice.
214
+ ///
195
215
/// [`Exp1`]: struct.Exp1.html
196
216
/// [`StandardNormal`]: struct.StandardNormal.html
197
217
#[ derive( Debug ) ]
0 commit comments