I have a vector v = {1,5,4,2}. Now I want to write a function that returns a random pair of numbers from this vector.
Example: {1,2},{4,4},{2,5},{5,1},{1,5},{2,2}....... I want this pair to be generated in a random manner.
Any idea how to implement this ?
auto pair = std::make_pair(v[rand() % v.size()], v[rand() % v.size()]);
is one way.
Switch out rand() to something from the new <random> library of C++11 if you require the generator to have better statistical properties: aside from the generator itself, the use of % can introduce a statistical bias.
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