Why Vec.shuffle() may produce the same result?

⚓ Rust    📅 2026-03-16    👤 surdeus    👁️ 2      

surdeus

I suppose this is more of natural of shuffle or pseudo-random generation issue. Anyway, here is my question.

I understand that normally it's because seeding with the same rng value, resulting in the same result without items being shuffled. However, my case is I already provide with ThreadRng by calling rng(). Actually the vec does shuffle. It is just sometime the result produced is the same. For instance, the code below produces ["b", "c", "a"] at the first run, but the second run it produces ["a", "b", "c"], which is the same as my input vec data. I appreciate any advice or suggestions. Thanks.

let mut vec: Vec<&str> = vec!["a", "b", "c"];
let mut r = rng();
vec.shuffle(&mut r);
println!("{:?}", vec) // this may print ["a", "b", "c"]

4 posts - 4 participants

Read full topic

🏷️ Rust_feed