Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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;

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, " "));

   
  
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}