I have tried calling rand() function in a loop that iterates n times, but the numbers generated are not unique. So please suggest any function or a logic to generate n random numbers from 0 to n.
-
The random numbers should be unique, or rather, change call-to-call. If your loop also includes a call to `srand`, then that's your problem. You should only call `srand` once at the beginning of your program. – Joe Z Dec 01 '13 at 06:07
-
1Also, you should consider looking into the C++11 random number engines / distributions if you can use C++11. I gave an overview in this answer: http://stackoverflow.com/questions/20309009/get-true-or-false-with-a-given-probability/20309151#20309151 – Joe Z Dec 01 '13 at 06:08
-
Use `std::iota` and `std::shuffle`. Unless I'm misunderstanding your "unique". Then it's just `std::generate`. – chris Dec 01 '13 at 06:08
-
To clarify Joe Z's comment, most rand() implementations have a period of 2^32 which means the results don't repeat until after about 4 billion calls unless you call srand() again. That said, if you're using `% n` to get a number between 0 and n-1 then for n << 2^32 those values can repeat at any time. It's meant to work that way. If you want the numbers from 0 to n-1 arranged in random order, then follow Chris's advice - put the sequence into a vector then use std::shuffle - it's like shuffling a deck of cards. – Tony Delroy Dec 01 '13 at 06:16
-
If you are trying to generate `n` random numbers from `[0, n)`, then your set of numbers is going to be every number from 0 to n ... there is no need for a random number generator at all. – Zac Howland Dec 01 '13 at 06:16
-
@ZacHowland, I just rolled a six-sided die six times. I got {2, 3, 3, 4, 6, 1}. No five. I think you meant to say "If you are trying to generate `n` *unique* random numbers from `[0, n)`, then your set of numbers is going to be every number from 0 to n". – chwarr Dec 01 '13 at 06:27
-
2@user3053803, can you clarify what you mean by "the numbers generated are not unique" and why you expected them to be unique? How are you planning to use these random numbers? Do you want the numbers in the sequence [0, n) but in a random order, perhaps? – chwarr Dec 01 '13 at 06:30
2 Answers
If you need to produce a random shuffle of the values [0,n) (ie. 0, 1, 2, ... n-1), then the following code (adapted from cppreference.com) will do the trick:
#include <numeric>
#include <algorithm>
#include <vector>
vector<int> shuffled_vector( int n )
{
std::vector<int> v(n);
std::iota(v.begin(), v.end(), 0);
std::random_shuffle(v.begin(), v.end());
return v;
}
The original example on cppreference was a bit more involved. Find it here: http://en.cppreference.com/w/cpp/algorithm/iota
You can provide a random number generator to std::random_shuffle
if you have a particular need. It's the third argument after v.end()
. More details here: http://en.cppreference.com/w/cpp/algorithm/random_shuffle

- 17,413
- 3
- 28
- 39
It depends highly on the probability distribution of your unique numbers combination you need to get.
The most straightforward (but slow) approach would be just generate random numbers, skipping clones, until you hot n
numbers. In this case you will have uniform probability distribution for unique n-numbers combination.

- 12,105
- 14
- 57
- 95