How the generated random number with no duplicates

have to make 6 random numbers generate 10 times in 1 run to show there are no duplicates

4th Dec 2016, 1:50 PM
Dunkin Banks
Dunkin Banks - avatar
1 Answer
+ 1
Have you seen - http://en.cppreference.com/w/cpp/numeric/random ? They've added some new functionality in C++ 11, now you can pick the distribution of the random numbers (uniform_int_distribution in your case I'm guessing). If you really want there to be no duplicates you can put every generated number in std::set and if your new random number is already in that set - keep regenerating it, until it isn't. Something like this: static std::set<int> numbersAlreadyGenerated; int generateNonRepeatingRandomNumber() { int randomNumber; do{ randomNumber = generateRandom(); }while (numbersAlreadyGenerated.find(randomNumber) != numbersAlreadyGenerated.end()); numbersAlreadyGenerated.insert(randomNumber); return randomNumber; }
3rd Jun 2017, 9:01 PM
Tom Cox
Tom Cox - avatar