Srand function in C ++

This code

#include <stdio.h> #include <stdlib.h> #include <time.h> int main () { printf ("First number: %d\n", rand() % 100); srand ( time(NULL) ); printf ("Random number: %d\n", rand() % 100); srand ( 1 ); printf ("Again the first number: %d\n", rand() %100); return 0; } 

has the following output:

 First number: 41 Random number: 13 Again the first number: 41 

There is also the following rule:

Two different initializations with the same seed instruct the pseudo-random generator to generate the same sequence of results for subsequent calls to rand in both cases.

I understand the words, but I just do not understand the method itself. Why is he back again? Is it random or should it return the same result in each case according to this code?

+6
c ++ random
source share
6 answers

If you call rand() without first calling srand() , the behavior is as if you were srand() with 1 as an argument.

This behavior is defined in the original C standard. I do not have a full copy of it, but Open POSIX standards are best suited as they include the full C standard (with some extensions):

http://www.opengroup.org/onlinepubs/000095399/functions/rand.html

The srand () function uses this argument as a seed for a new sequence of pseudo-random numbers that will be returned by subsequent calls to rand (). If srand () is called with the same initial value, the sequence of pseudo random numbers must be repeated. If the rand () call is called before any srand () calls are made, the same sequence should be generated as when srand () was first called with an initial value of 1.

The actual result of calling rand() for any given seed is implementation-specific, except that for any seed x, calling n in rand() after sowing the generator should return the same result. Therefore, at runtime, you will always get 41 on the first call to rand() after calling srand(1) , and you will always get the same result (whatever it is) for the second call to rand() , other implementations may use different algorithms, which gives different results.

+7
source share

The C ++ standard refers to the C standard for rand and srand (see section 26.8 C ++ 0x):

The rand function has the semantics specified in the C standard, except that the implementation may indicate that certain library functions may call rand .

Standard C (7.20.2.2 of C99) is pretty categorical:

If rand is called before any srand calls have been made, then the same sequence should be generated as the first call to srand with an initial value of 1.

So, the first time you call rand , the seed is 1. This is also 1 the third time you call it, so you get the same value.

The second time you called rand , the seed was set based on the current time, so you got a different result. It is usually not recommended to reseed a random number generator if you really do not want the same sequence (for example, when testing).

+7
source share

I think this may be a problem:

If the seed is set to 1, the generator is reinitialized to its initial value and returns the same values ​​as before any rand or srand call.

link srand

0
source share

Calling srand with a value of 1 resets the generator to its initial state when the program starts.

0
source share

srand(3) allows you to get repeatable output from a random number generator. This way you can write random number dependent programs, but test them deterministically. (As a rule, having a command line parameter for calling srand() with the argument provided, so it can be run a hundred times with inputs 1, 2, 3, 4, 5, ...).

So, this behaves as expected.

0
source share

Probably because in your first call to rand, the random number generator was not seeded, so it uses the default value of 1 for its seed.

0
source share

All Articles