Is there some logic or principle behind that?
Are the numbers generated completely random?
Suppose I am running this program:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsigned int) seconds);
/*
Output random values.
*/
cout<< rand() << endl;
cout<< rand() << endl;
cout<< rand() << endl;
return 0;
}
What it shows: http://img14.imageshack.us/img14/1538/98271820.png
It showed 205 twice.
The question was basically answered in comments and another answer, but I'll gather it up in one place.
C++ rand()
function produces not a truly random sequence of numbers, but a pseudo-random one. This means that it is basically a pre-defined sequence of numbers which are "random", but fixed somewhere (actually, it's more complex than that, but this is a simplification for better understanding). Think of it as a long list of integers.
Each call to rand()
function pulls the current number and moves the pointer to "current "random" number" to the next one.
What srand()
function does is basically setting the pointer to some location in the list. If you don't call the srand()
function on each launch, or call it with fixed parameter (seed), you will have the same sequence of numbers on each program launch.
When you're setting your seed from the seconds, if you launch your program twice within that second, your seed will be the same - hence producing the same result.
Try the following code:
#include <windows.h>
// << other code >>
for (int i=0; i<50; i++) {
time(&seconds);
srand(seconds);
cout<< seconds<<" "<<rand()<<endl;
Sleep(100);
}
You will notice, that each "seconds" value correspond to some fixed "first" value for the rand()
function.
Starting with the second question:
Are the numbers generated completely random?
No, that is very unlikely to ever happen in a computer. They are "pseudo-random" numbers, which is some sequence of numbers that vary in range over time in a random-like fashion. But if you start with the same "seed", you get the same sequence each time. This predictability is sometimes very useful, as it allows repeating the same experiment several times with the same outcome - altering the seed, will allow a similar run to have a different outcome.
The function srand
sets the seed. Some systems do have a function called randomize
, but it is not part of the standard as such. If it does exist it sets the seed to something unknown to the code - such as the current time in milliseconds.
Is there some logic or principle behind that?
Yes. There are several methods for generating pseudo-randum numbers. Simple ones can be written in one or two lines of C code using regular int
or long
types, and just consists of taking the "current value" + some constant, multiplied by some large number and modulo some other large number.
More complex ones involve dozens of more lines of rather complicated math with large numbers - for example Mersenne Twister is a recent work that is available as source code if you search a little bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With