8

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?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Matthew
  • 4,477
  • 21
  • 70
  • 93
  • 3
    [`srand()`](http://www.cplusplus.com/reference/clibrary/cstdlib/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](http://www.cplusplus.com/reference/clibrary/cstdlib/RAND_MAX/). – WhozCraig Nov 07 '12 at 16:50

4 Answers4

16

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.

EDIT:

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;
}
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • 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 07 '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 07 '12 at 17:00
  • When I use this (for small ranges of numbers) I get consistent repitions, say I wish to get a random number between 0 and 5; I'll get in sequence 0 five times, 1 five times, 2 five times, etc. – RaenirSalazar Nov 06 '13 at 08:37
5

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.

tomahh
  • 13,441
  • 3
  • 49
  • 70
1

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

Community
  • 1
  • 1
gda2004
  • 718
  • 13
  • 32
1

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:

The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive

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().

srand(time(NULL))

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

Paul Rubel
  • 26,632
  • 7
  • 60
  • 80