Function smartnoise_runtime::utilities::noise::sample_uniform [−][src]
pub fn sample_uniform(
min: f64,
max: f64,
enforce_constant_time: bool
) -> Result<f64>
Returns random sample from Uniform[min,max).
All notes below refer to the version that samples from [0,1), before the final scaling takes place.
This algorithm is taken from Mironov (2012) and is important for making some of the guarantees in the paper.
The idea behind the uniform sampling is to first sample a “precision band”. Each band is a range of floating point numbers with the same level of arithmetic precision and is situated between powers of two. A band is sampled with probability relative to the unit of least precision using the Geometric distribution. That is, the uniform sampler will generate the band [1/2,1) with probability 1/2, [1/4,1/2) with probability 1/4, and so on.
Once the precision band has been selected, floating numbers numbers are generated uniformly within the band by generating a 52-bit mantissa uniformly at random.
Arguments
min
: f64 minimum of uniform distribution (inclusive)
max
: f64 maximum of uniform distribution (non-inclusive)
Return
Random draw from Unif[min, max).
Example
// valid draw from Unif[0,2) use smartnoise_runtime::utilities::noise::sample_uniform; let unif = sample_uniform(0.0, 2.0, false);
// fails because min > max use smartnoise_runtime::utilities::noise::sample_uniform; let unif = sample_uniform(2.0, 0.0, false);