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"]
When you trow 3 dices, two times, both results can be identical. I assume it is the same for shuffle, unless you actively prevent this case.
[EDIT]
If this hint was not clear enough: Shuffling a vector with N elements is typically implemented by swapping each position i with a random position between 0 and N-1. Obviously, this random position can be identical to i, so no visible change occurs. And when you have only 3 items, it might occur that for all 3 positions no visible change happens (or that a subsequent swap undoes a previous one). The shuffle algorithm typically takes no special care to avoid this case.
Three element vector has only 6 possible permutations. So it is quite likely (about 16%) that after shuffling it will have the original permutation. Have you tried running this test many times and looking at permutation distribution? Documentation of SliceRandom::shuffle (from rand crate) says:
The resulting permutation is picked uniformly from the set of all possible permutations.
So if shuffle operation does not change the order of elements 1/6th of the time (or rather after changes, relative order of all elements is preserved), then everything works as it should.
This is a good example of how random shuffling (picking a random permutation from a uniform distribution) may not match the intuitive expectation of what "shuffling" is. This is well known by creators of audio players – what people actually want when they toggle shuffle play is not a random permutation but something with more constraints.
(Also, it's worth noting that the likelihood of drawing any given permutation from a uniform distribution decreases extremely quickly as the number of elements grows. For a three-element list you're 17% likely to end up with the same order, but with ten-elements the likelihood already drops to one in 3.6 million, and with twenty elements, getting the same list back is astonishingly unlikely, even after years of continuous shuffling.)