Skip to main content

Module rand

Module rand 

Source
Expand description

Random floating-point number generation with the rand crate.

There are two distributions for generating random floats. Uniform01 generates floats between 0 and 1 (it also backs rand’s Standard / Open01 / OpenClosed01). UniformFBig generates floats in a given range and is the backend for rand’s SampleUniform trait.

The distributions and their sampling algorithms are defined here once, generic over the BitRng trait. Each rand version’s Distribution / UniformSampler / SampleUniform impls live in the rand_v08 / rand_v09 / rand_v010 modules (enable the matching feature); adapt that version’s RNG with dashu_int::rand::bridge_v08 / bridge_v09 / bridge_v010. See those modules for usage examples.

§Precision and rounding

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

  • Uniform01 generates floats with the precision decided by the constructor.
  • The builtin rand distributions (Standard / StandardUniform, Open01, OpenClosed01) generate floats with the max precision such that the significand fits in a DoubleWord.
  • UniformFBig (and therefore rand’s Uniform) generates floats with the precision being the maximum between the interval boundaries.

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

Structs§

Uniform01
A uniform distribution between 0 and 1. It can be used to replace the Standard, Open01, OpenClosed01 distributions from the rand crate when you want to customize the precision of the generated float number.
UniformFBig
The back-end implementing rand::distributions::uniform::UniformSampler for FBig (and DBig).