Skip to content

Generating random numbers with rand::rng() is very slow #4297

@phkeese

Description

@phkeese

Using this example:

use rand::Rng;

#[cfg(test)]
mod tests {
    use std::hint::black_box;
    use rand::Rng;

    fn generate_random(n: usize) {
        let mut rng = rand::rng();
        for i in 0..n {
            black_box(rng.random_bool(0.5));
        }
    }

    #[test] fn gen_10() { generate_random(10); }
    #[test] fn gen_100() { generate_random(100); }
    #[test] fn gen_1000() { generate_random(1000); }
    #[test] fn gen_10000() { generate_random(10_000); }
    #[test] fn gen_100000() { generate_random(100_000); }
}

to generate an increasing number of random bools takes much longer with Miri than without.
Running without Miri completes in less than a second, with Miri, even just 10000 takes multiple seconds, generating 100,000 does not seem to finish in 30 seconds.
Is there a way of speeding up random value generation?
I tried MIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri test but it did not speed up the process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions