C++ Way To Randomly Select M Numbers
Join the DZone community and get the full member experience.Join For Free
This will randomly select M numbers from the interval [1, NMAX]. Note the extensive use of STL.
const short NMAX = 49; const short M = 6; std::vector
v; for (short i(0); i < NMAX; ++i) v.push_back(i + 1); random_shuffle(v.begin(), v.end()); copy(v.begin(), v.begin() + M, std::ostream_iterator (std::cout, " "));
Opinions expressed by DZone contributors are their own.