Over a million developers have joined DZone.

C++ Way To Randomly Select M Numbers

DZone's Guide to

C++ Way To Randomly Select M Numbers

· ·
Free Resource
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;

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.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}