# How to generate random numbers the same everytime

• 04-09-2009
lehe
How 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-2009
Cpro
Quote:

Originally Posted by lehe
... the same random numbers everytime I run my program? And not the same everytime?

I'm not sure that makes sense. Could you please clarify.
• 04-09-2009
Drogin
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;```
If you execute this program several times, you will have the exact same output.

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.

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

Code:

```#include <iostream> #include <cstdlib> int main() {   int theNumber = ( 1 + rand() % 10 );   std::cout << theNumber << std::endl;   return 0; }```
You do not include srand() or <ctime> at all. If rand() is not seeded, it will produce the same results each time. Place a loop around your code to see the desired effects.
• 04-09-2009
matsp
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; }```
So if seed is the same value, then the random numbers will be the same ones every time.
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-2009
EVOEx
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-2009
cyberfish
For example, the Mersenne Twister.

It has a free reference implementation, too, that is very easy to use.
• 04-09-2009
Drogin
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-2009
cyberfish
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-2009
Drogin
Ye, but how can the implementation bother our portability, as long as the implementation stay true to the ISO Standard?
• 04-09-2009
cyberfish
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-2009
Drogin
Quote:

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.
Aaah, I'm with you now :)