Rand () and srand () in C ++

What is the basis of random number generation in C ++?

Is there any logic or principle?

Are the generated numbers completely random?

Suppose I run 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

He showed 205 twice.

+7
source share
2 answers

Starting from the second question:

Are the generated numbers completely random?

No, this is unlikely to ever happen on a computer. They are “pseudo-random” numbers, which are some sequence of numbers that change randomly over time. But if you start with the same “seed”, you get the same sequence every time. This predictability is sometimes very useful, because it allows you to repeat the same experiment several times with the same result - with a change in the seed, which will allow a similar run to have a different result.

The srand function sets the seed. Some systems have a function called randomize , but it is not part of the standard as such. If it exists, it sets the seed to something unknown code - for example, the current time in milliseconds.

Is there any logic or principle?

Yes. There are several ways to generate pseudo-row numbers. Simple ones can be written in one or two lines of C code using the usual int or long types, and simply consists in taking the "current value" + some constant multiplied by some large number and modulo some other large number.

The more complex ones include dozens of other lines of fairly complex math with large numbers - for example, Mersenne Twister is a recent work available as source code if you look a bit.

+1
source

The question was mostly answered in the comments and another answer, but I will collect it in one place.

The C ++ rand() function does not create a truly random sequence of numbers, but a pseudo-random one. This means that it is basically a predefined sequence of numbers that are "random" but fixed somewhere (actually it’s more complicated, but it’s simplification for a better understanding). Think of it as a long list of integers.

Each call to the rand() function prints the current number and moves the pointer to the "current" random "number" to the next.

What the srand() function does is basically set a pointer to some place in the list. If you do not call the srand() function each time you start it or do not call it with a fixed parameter (seed), you will have the same sequence of numbers each time the program starts.

When you set your seed from seconds, if you run your program twice during this second, your seed will be the same - hence, getting the same result.

Try using 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 corresponds to some fixed “first” value for the rand() function.

+6
source

All Articles