Skip to main content

random_positive_floats_with_precision

Function random_positive_floats_with_precision 

Source
pub fn random_positive_floats_with_precision(
    seed: Seed,
    mean_sci_exponent_abs_numerator: u64,
    mean_sci_exponent_abs_denominator: u64,
    prec: u64,
) -> RandomPositiveFiniteFloats<UniformRandomNaturalRange> 
Expand description

Generates random positive finite Floats with a specified precision.

Simpler Floats (those with a lower absolute sci-exponent) are more likely to be chosen. You can specify the mean absolute sci-exponent by passing the numerators and denominators of its means.

But note that the specified mean is only approximate, since the distribution we are sampling is truncated geometric, and its exact means are somewhat annoying to deal with. The practical implications are that

  • The actual mean is slightly lower than the specified mean.
  • However, increasing the specified mean increases the actual mean, so this still works as a mechanism for controlling the sci-exponent.
  • The specified sci-exponent mean must be greater than 0, but it may be as high as you like.

Neither positive nor negative zero is generated. NaN is not generated either.

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.

§Examples

use itertools::Itertools;
use malachite_base::random::EXAMPLE_SEED;
use malachite_float::float::random::random_positive_floats_with_precision;
use malachite_float::ComparableFloat;

// The number after the '#' is the precision.
assert_eq!(
    random_positive_floats_with_precision(EXAMPLE_SEED, 10, 1, 10)
        .take(20)
        .map(|f| ComparableFloat(f).to_string())
        .collect_vec()
        .as_slice(),
    &[
        "0.95898#10",
        "1.8887e-6#10",
        "0.012909#10",
        "0.70996#10",
        "1.0202e5#10",
        "0.011810#10",
        "0.019531#10",
        "3.0820#10",
        "7.2422#10",
        "0.000055969#10",
        "0.38770#10",
        "21440.0#10",
        "58560.0#10",
        "1.4297#10",
        "62.188#10",
        "0.46582#10",
        "0.0016594#10",
        "1914.0#10",
        "0.13599#10",
        "0.0011444#10"
    ]
);