Tell me more ×

I have the following method which generates a random number:

int random_number() //Random number generator
    int x = rand() % 1000000 + 1; //Generate an integer between 1 and 1000000
    return x;

The call to this method is used in a loop which iterates five times. The problem with this method is that it always seems to generate the same numbers when running the program several times. How can this be solved please?

share|improve this question
srand() may come in handy. However, you may be depressed to take that modulo when/if you discover 1000000 is significantly larger than most implementation definitions of RAND_MAX. – WhozCraig Nov 7 '12 at 16:50

4 Answers

up vote 3 down vote accepted

You need to seed the random number generator, such as:

srand ( time(NULL) );
int x = rand() % 1000000 + 1;

Seeding the pseudorandom number generator essentially decides on the random number set that it will iterate through. Using the time is the standard method of achieving adequately random results.


To clarify, you should seed only once and get many random numbers, something like this:

srand ( time(NULL) );
loop {
    int x = rand() % 1000000 + 1;

Rather than something like:

loop {
    //Particularly bad if this line is hit multiple times in one second
    srand ( time(NULL) ); 
    int x = rand() % 1000000 + 1;
share|improve this answer
Your suggestion worked but it seems that the number does not change from one loop to the next. Therefore all the files end up having the same name – Matthew Nov 7 '12 at 16:58
If you are iterating a loop, don't call srand on each iteration. Call it only once, and rand numerous times. If you call both each time, any calls within the same second will return the same number (since time(NULL) will return the same value). – femtoRgon Nov 7 '12 at 17:00
Thanks. That solved the problem :) – Matthew Nov 7 '12 at 17:02

make a call to srand(time(NULL)); when your program launches.

srand sets a seed to the rand function. Giving it the return value of time(NULL) helps getting a different seed at each program run.

As you tagged your question as c++, you could either use c++11 feature to handle random number generation.

share|improve this answer
Your suggestion worked. However, all the five files now have the same name :s – Matthew Nov 7 '12 at 16:57
what files (are you talking about) ? – Tom Ahh Nov 7 '12 at 16:58

femtoRgon is right. This will seed the program but take a look at the new c++ standard as they have improved random number generation see

Random numbers in C++0x

share|improve this answer

rand is not really a random number, but rather a pseudo-random one that just "looks" random if you don't know the algorithm used to generate the values. From the man page:


pseudo-random means that given the same input, called a seed, it will give the same output. This is actually quite useful when you're trying to debug a problem since the same "random" values will be returned, letting you reproduce the problem. It's bad if you really need randomness.

As noted above by changing the seed to something different on each run, like say seconds since the epoch, you can get different values from your call to rand().


If you're trying to debug you may want to print out the seed so you can reproduce problems if they occur.

share|improve this answer

Your Answer


