Rand::thread_rng , rng.gen() // returning same values


#1

I have a piece of code like

let mut rng = rand::thread_rng()
let ans = Vec<f32>::new();
ans.push(rng.gen());
ans

Now, unfortunately, across multiple runs, the vector is returning the same list of random numbers (as if it’s using the same seed). How do I get it to use the current time (or some os randomness) as the initial seed?


#2

I can’t reproduce the problem on playground

by default thread_rng will initialize it self from the OS and will periodically reseed it self, so this shouldn’t be an issue.

docs for thread_rng

If the problem persists, then you can file an issue on rand’s github here


#3

Thank you for the rust playground example.

Sorry for the false alarm. crate rand is also generating different random numbers across different runs for me. The bug was elsewhere.