Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Alexandru Scvortov

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

C++ way to randomly select M numbers

This will randomly select M numbers from the interval [1, NMAX].

Note the extensive use of STL.

   1  
   2  const short NMAX = 49;
   3  const short M = 6;
   4  
   5  std::vector<short> v;
   6  for (short i(0); i < NMAX; ++i)
   7  	v.push_back(i + 1);
   8  
   9  random_shuffle(v.begin(), v.end());
  10  copy(v.begin(), v.begin() + M, std::ostream_iterator<short>(std::cout, " "));
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS