I have two vectors with the same number of elements, but their types have completely different sizes. I need to shuffle them so that both have the exact same order after shuffling (each element in one vector is related each element in the other). The way I found to do it was:
// sizeof(a[0]) != sizeof(b[0])
// a.size() == b.size()
{
std::mt19937 g(same_seed);
std::shuffle(a.begin(), a.end(), g);
}
{
std::mt19937 g(same_seed);
std::shuffle(b.begin(), b.end(), g);
}
Can I rest assured that both vectors will be shuffled the same way? Is this implementation dependent? Do I have such guarantee from std::shuffle
specification?
There is an interesting statement about shuffle in the specification:
Remarks: To the extent that the implementation of this function makes use of random numbers, the object
g
shall serve as the implementation’s source of randomness.
Even so, that statement doesn't help you. The "Remarks" section is normative text, so this is saying that g
will provide the random numbers to determine the shuffled order. However, it does not declare that g
is the only factor which determines the permutation.
While the size of the container's value probably doesn't matter, there's no guarantee that some property of the type wouldn't affect something. For example, if the value type were trivially copyable, an implementation might use a different version of the function that used a slightly different algorithm. However, if it were a register-sized value, it might not.
In short, no, std::shuffle
does not guarantee what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With