# Thread: Does it guarantee that random numbers generated are different with each other.

1. ## Does it guarantee that random numbers generated are different with each other.

Dear all,

I have one question about generating random number. My snippet of code is as follows,

Code:
```srand(0);

for (i = 0; i< 10,000,000; i++)
{
random_numbers[i] = rand();
}```
My case requirement is that each value in the set of generating random number should be different.

However, I am not sure whether or not even the upper snippet code will acheive this goal?

Any suggestion, hint?

2. Originally Posted by qingxing2005
srand(0);
Don't do this when generating random numbers. The srand parameter is called a "seed". If you want a different sequence of numbers every time you run your program, use

Code:
`srand((unsigned)time(0));`
time() is defined in time.h, by the way

As for your question, your snippet does not guarantee that the generated numbers will be different. In fact, generating 10000000 values doesn't help either. With rand(), you can only generate numbers between 0 and RAND_MAX, where RAND_MAX is 32767 for most compilers. My suggestion would be to use a vector, but there are no vectors in C, so perhaps you should give it a go on implementing one yourself or wait for other suggestions.

3. some compilers use RAND_MAX value == 32000 (don't remember the exact value, but close)
so on this compilers you will never receive 10000000 different values

also, even if you get some rand() fuction that provides a range you need it will never garantee the uniquness of the generated values...

So if you want unique numbers use some other approches...

look forum post for shuffle function - you will find some samples that might be helpful

4. some compilers use RAND_MAX value == 32000 (don't remember the exact value, but close)
Probably 2^15-1, 32767, which is also SHRT_MAX on most compilers. (Mr_Miguel already mentioned this.)

Though in fact most compilers use INT_MAX for RAND_MAX, so it might be 2^31-1. But that's not a good thing to rely on.

Code:
`for (i = 0; i< 10,000,000; i++)`
Putting commas in a number doesn't do what you want. Leave out the commas.

5. Originally Posted by dwks
Probably 2^15-1, 32767, which is also SHRT_MAX on most compilers. (Mr_Miguel already mentioned this.)

Though in fact most compilers use INT_MAX for RAND_MAX,
MSVC6 and MSVC++2005 Expert use 32767

6. Dev-C++, GCC (many versions, I have 4 versions which all do), and many other compilers use 2^31-1.

7. Use the Mersenne Twister generator for a guaranteed larger range.Then generate an array of integers from 1 to X, and perform a Knuth shuffle with the rand generator on the array. This guarantees a random permutation of X unique numbers.

8. Originally Posted by dwks
Dev-C++, GCC (many versions, I have 4 versions which all do), and many other compilers use 2^31-1.
Strange, Dev-C++ isn't using vanilla MinGW? MinGW has always had
#define RAND_MAX 0x7FFF
in stdlib.h on my computer.

9. non-repeating "random" numbers are not random. Random means that there is an equal chance for each of all possible numbers in the range. In other words, if you get a 7, the next time you generate a number, 7 should still be one of the possiblilities, it should not be excluded. Random numbers can repeat.

What you seem to want is analogous to dealing cards from a shuffled deck. You probably want a large array that is first "shuffled" and then "dealt".

10. Originally Posted by dwks
Probably 2^15-1, 32767, which is also SHRT_MAX on most compilers. (Mr_Miguel already mentioned this.)

Though in fact most compilers use INT_MAX for RAND_MAX, so it might be 2^31-1. But that's not a good thing to rely on.

Code:
`for (i = 0; i< 10000000; i++)`
Putting commas in a number doesn't do what you want. Leave out the commas.
Hey, in my piece of code, there ie only 10000000 used. I donnot so whether this number appeared. :0)

11. RAND_MAX is a maxmimum number returned by the rand()

And because rand() returns numbers from the interval [0,RANDMAX] it also describes the number of different values that can be received from this function

12. > [0,RANDMAX]
Umm . . . perhaps you mean (0,RANDMAX]. Or [0,RANDMAX). I can never remember which.

13. The rand function returns a pseudorandom integer in the range 0 to RAND_MAX
both 0 and RAND_MAX are possible return values for rand()

14. You're right, sorry.