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
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!
I'm not exactly sure if I understood your question, but here's my shot:
If you execute this program several times, you will have the exact same output.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
Similar to Drogin:
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.Code:#include <iostream>
#include <cstdlib>
int main() {
int theNumber = ( 1 + rand() % 10 );
std::cout << theNumber << std::endl;
return 0;
}
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):
So if seed is the same value, then the random numbers will be the same ones every time.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
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)
For example, the Mersenne Twister.
It has a free reference implementation, too, that is very easy to use.
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...
C standard says rand must give you a sequence of pseudo-random numbers based on a seed. Implementors get to choose the actual algorithm.
Ye, but how can the implementation bother our portability, as long as the implementation stay true to the ISO Standard?
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.
Aaah, I'm with you now :)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.