pub struct Qrng<T: Quasirandom> { /* fields omitted */ }
Expand description

Main driver of this library

QRNG vs PRNG

A Qrng is a quasirandom value generator. Rather than generating values that are truly random (or pseudorandom), it produces values that are evenly distributed across the domain of all possible values.

Uses

Qrngs are particularly useful for things like monte-carlo simulations, where evenly covering the domain improves convergence.

Features

A Qrng can be built for any tuple up to size 32 for which all elements implement FromUniform.

For instance, a Qrng<(f64, u32, bool, Option<i16>)> will generate values of the 5-tuple that, over enough samples, will uniformly cover that space.

Note

Type inference will typically force you to specify the type at construction time, e.g. Qrng::<(f64, f64)>::new(seed).

Example usage

use quasirandom::Qrng;
 
fn compute_pi() {
    let mut qrng = Qrng::<(f64, f64)>::new(0.123);
    let mut hits = 0.0;
    let mut total = 0.0;
    for _ in 0..1_000_000 {
        let (x, y) = qrng.gen();
        if x.hypot(y) < 1.0 {
            hits += 1.0;
        }
        total += 1.0;
    }
    println!("pi is approximately {}", 4.0 * hits / total);
}

Acknowledgments

The technique used in this generator is directly taken from this blog post by Martin Roberts.

Implementations

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.