@@ -236,7 +236,7 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
236
236
/// use rand::distributions::Uniform;
237
237
///
238
238
/// let val: f32 = SmallRng::new().sample(Uniform);
239
- /// println!("f32 from [ 0,1): {}", val);
239
+ /// println!("f32 from ( 0,1): {}", val);
240
240
/// ```
241
241
///
242
242
/// With dynamic dispatch (type erasure of `Rng`):
@@ -248,9 +248,29 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
248
248
/// let mut rng = thread_rng();
249
249
/// let mut erased_rng: &mut RngCore = &mut rng;
250
250
/// let val: f32 = erased_rng.sample(Uniform);
251
- /// println!("f32 from [ 0,1): {}", val);
251
+ /// println!("f32 from ( 0,1): {}", val);
252
252
/// ```
253
253
///
254
+ /// # Open interval for floats
255
+ /// In theory it is possible to choose between an open interval `(0, 1)`, and
256
+ /// the half-open intervals `[0, 1)` and `(0, 1]`. All can give a distribution
257
+ /// with perfectly uniform intervals. Many libraries in other programming
258
+ /// languages default to the closed-open interval `[0, 1)`. We choose here to go
259
+ /// with *open*, with the arguments:
260
+ ///
261
+ /// - The chance to generate a specific value, like exactly 0.0, is *tiny*. No
262
+ /// (or almost no) sensible code relies on an exact floating-point value to be
263
+ /// generated with a very small chance (1 in ~8 million (2^23) for `f32`, and
264
+ /// 1 in 2^52 for `f64`). What is relied on is having a uniform distribution
265
+ /// and a mean of `0.5`.
266
+ /// - Several common algorithms rely on never seeing the value `0.0` generated,
267
+ /// i.e. they rely on an open interval. For example when the logarithm of the
268
+ /// value is taken, or used as a devisor.
269
+ ///
270
+ /// In other words, the guarantee some value *could* be generated is less useful
271
+ /// than the guarantee some value (`0.0`) is never generated. That makes an open
272
+ /// interval a nicer choice.
273
+ ///
254
274
/// [`Exp1`]: struct.Exp1.html
255
275
/// [`StandardNormal`]: struct.StandardNormal.html
256
276
#[ derive( Debug ) ]
0 commit comments