Hi,

How can I generate the same random numbers everytime I run my program? And not the same everytime? Does it have something to do with the seed for the random number generator? I am using rand().

Thanks and regards!

Printable View

- 04-09-2009leheHow to generate random numbers the same everytime
Hi,

How can I generate the same random numbers everytime I run my program? And not the same everytime? Does it have something to do with the seed for the random number generator? I am using rand().

Thanks and regards! - 04-09-2009Cpro
- 04-09-2009Drogin
I'm not exactly sure if I understood your question, but here's my shot:

Code:`int seed = someNumber;`

srand(seed); // Set the seed

cout << rand() <<endl;

cout << rand() <<endl;

cout << rand() <<endl;

So the rand() function gives random numbers in a sequence, based on a seed.

If you want random**sequences**, you will have to use a "random" seed.

You can do this by, for example, using the current time as the seed.

For more info, see:

rand - C++ Reference - 04-09-2009swgh
Similar to Drogin:

Code:`#include <iostream>`

#include <cstdlib>

int main() {

int theNumber = ( 1 + rand() % 10 );

std::cout << theNumber << std::endl;

return 0;

}

- 04-09-2009matsp
Right, so rand() and srand() does something like this (it is not necessarily how it does it in detail, but the PRINCIPLE applies - the actual code may be a bit more complicated):

Code:`static seed = largeNumber0;`

int rand()

{

seed = seed * largenumber1 + largenumber2;

return seed % MAX_RAND;

}

void srand(int x)

{

seed = x;

}

The large numbers are almost always prime numbers or pseudo-primes (that is "nearly primes").

In fact, the SEQUENCE of numbers you get out of rand() will ALWAYS be the same, it's just where in the [hopefully pretty long] sequence you start that will change by modifying the seed.

--

Mats - 04-09-2009EVOEx
A possibly valuable addition:

These methods work, but are not portable. Another implementation of srand/rand may be different. If you want it to work portably, implement your own random number generator (there are several good algorithms available on the web) - 04-09-2009cyberfish
For example, the Mersenne Twister.

It has a free reference implementation, too, that is very easy to use. - 04-09-2009Drogin
Could someone explain why it's not portable?

I thought this function was defined in the C standard library, so I guess the definition in the standard is so strict it enforces portability anyway?

I mean, what else would standards be good for... - 04-09-2009cyberfish
C standard says rand must give you a sequence of pseudo-random numbers based on a seed. Implementors get to choose the actual algorithm.

- 04-09-2009Drogin
Ye, but how can the implementation bother our portability, as long as the implementation stay true to the ISO Standard?

- 04-09-2009cyberfish
It doesn't. As long as you don't make the assumption that the same seed will give you the same sequence everywhere. That is not gauranteed by the standard.

- 04-09-2009DroginQuote:

As long as you don't make the assumption that the same seed will give you the same sequence everywhere. That is not gauranteed by the standard.