macroquad icon indicating copy to clipboard operation
macroquad copied to clipboard

Unintended behavior of rand::gen_range()

Open cenysor opened this issue 6 months ago • 3 comments

I discovered an unintended behavior of rand::gen_range()

The following code

use macroquad::prelude::*;

fn main() {
    let mut count = 0; 
    loop {
        if rand::gen_range(1, 5) == 5 {
            break;
        }
        count += 1;
    }

    println!("{count}");
}

reproducibly prints 14540063

Expected behavior: loop running forever

macorquad version 0.4.4 rustc 1.75.0 compiled on Linux Mint 21

cenysor avatar Feb 15 '24 12:02 cenysor

you can use rand::srand(), something like macroquad::rand::srand(macroquad::miniquad::date::now()) should do the trick

not-fl3 avatar Feb 15 '24 21:02 not-fl3

@not-fl3 Latest quad-rand commit fixed this, but it is not updated on crates.io. My guess is because I did not bump up the crate version, but not I'm sure (I have no experience with publishing crates)

vdrn avatar Mar 19 '24 17:03 vdrn