Skip to main content

normal_random_floats

Function normal_random_floats 

Source
pub fn normal_random_floats(
    seed: Seed,
    prec: u64,
    rm: RoundingMode,
) -> NormalRandomFloats<RandomPrimitiveInts<u64>> 
Expand description

Generates random Floats sampled, with rounding, from the normal distribution with mean 0 and variance 1.

The result is a correctly-rounded sample: each output is a precision-prec Float, and the probability of any output equals the probability that a normally-distributed real number rounds to it under rm. The sampler is algorithm N of Karney, “Sampling exactly from the normal distribution”, as used by mpfr_nrandom; it is built entirely from Bernoulli trials on lazily-decided uniform deviates and draws no transcendental function evaluations. The number of random bits consumed is finite with probability 1 but not bounded. Every output is nonzero: a zero would require underflow, whose probability is on the order of $2^{-2^{30}}$. The result is never exact, so Exact is not a valid rounding mode.

The output length is infinite.

§Expected complexity per iteration

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is prec.

§Panics

Panics if prec is zero or if rm is Exact.

§Examples

use itertools::Itertools;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::float::random::normal_random_floats;
use malachite_float::ComparableFloat;

// The number after the '#' is the precision.
assert_eq!(
    normal_random_floats(EXAMPLE_SEED, 10, Nearest)
        .take(20)
        .map(|f| ComparableFloat(f).to_string())
        .collect_vec()
        .as_slice(),
    &[
        "-0.45166#10",
        "-2.2695#10",
        "-2.1602#10",
        "-0.78516#10",
        "0.23486#10",
        "-0.61230#10",
        "-0.91797#10",
        "-0.13672#10",
        "1.2891#10",
        "-0.045227#10",
        "-0.77051#10",
        "-0.21143#10",
        "0.61621#10",
        "-0.58594#10",
        "0.57520#10",
        "1.0117#10",
        "0.58008#10",
        "1.0195#10",
        "0.89453#10",
        "-0.069092#10"
    ]
);