Module dashu_float::rand

source ·
Expand description

Random floating point number generation with the rand crate.

There are two new distributions for generating random floats. The first one is Uniform01, which supports generating floats between 0 and 1. This is also the underlying implementation of the builtin rand distributions Standard, Open01, OpenClosed01. The other one is UniformFBig, which supports generating floats in a certain range. This is also the backend for the SampleUniform trait.

§Examples

use dashu_float::rand::Uniform01;

type FBig = dashu_float::FBig;

// generate FBigs in a [0, 1) or [0, 1] with a given precision
let a: FBig = thread_rng().sample(Uniform01::new(10));
let b: FBig = thread_rng().sample(Uniform01::new_closed(10));
let c: FBig = thread_rng().gen(); // the default distribution generates in [0, 1)
assert!(a >= FBig::ZERO && a < FBig::ONE);
assert!(b >= FBig::ZERO && b <= FBig::ONE);
assert!(c >= FBig::ZERO && c < FBig::ONE);

// generate FBigs in a range
let a = thread_rng().gen_range(FBig::from(3)..FBig::from(10));
let b = thread_rng().sample(Uniform::new(FBig::from(-5), &a));
assert!(a >= FBig::from(3) && a < FBig::from(10));
assert!(b >= FBig::from(-5) && b < a);

§Precision and Rounding

The precision of a float generated by different distributions is explained below:

  • Uniform01 will generate floats with the precision decided by the constructor.
  • Standard, Open01, OpenClosed01 will generate floats with the max precision such that the significand fits in a DoubleWord.
  • UniformFBig (and therefore Uniform) will generate floats the precision being the maximum between the interval boundaries.

The rounding of the FBig type doesn’t affect the number generation process.

Structs§