Skip to content

StepRng almost always produces bool false #1303

@jayvdb

Description

@jayvdb

I was trying to use StepRng to produce bool true, and found that I much harder than expected.
i.e. the following assertion is almost always true.

for i in 0..usize::MAX as u64 {
    for j in [0, 1, i] {
        let mut rng = rand::rngs::mock::StepRng::new(i, j);
        for k in 0..1000 {
            let foo: bool = rng.gen();
            if foo {
                eprintln!("{i} {j} {k}");
            }

            assert!(!foo);
        }
    }
}

I was able to get a true when "i=2147484" "j=2147484" & "k=999".

(edit: it becomes true when the start is 4013646938112, and then is consistently true, for a while at least)

I guess based on response for #1248 that there isn't much love for StepRng, and maybe this is just an unfortunate effect of StepRng

My real motivation is looking for a solution to cksac/fake-rs#128. I am trying to build a generator that is reasonably random most of the time, except when asked to produce a bool, it should always produce true. Is this possible?

My crude guess was if I could eliminate 0 from the rng responses, this would eliminate generating false, and StepRng seemed like a useful way to experiment with this idea.

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