Function srand in C++

2019-03-29 04:13发布

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, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

I understand the words but I just don't understand the method itself. Why did it return 41 again? Is it random or must it return the same result in every case according to this code?

标签: c++ random
6条回答
甜甜的少女心
2楼-- · 2019-03-29 04:17

srand(3) lets you get repeatable output from the random number generator. This way, you can write programs that depend on random numbers, but test them deterministically. (Typically by having a command-line option to call srand() with a supplied argument, so it could be run a hundred times with 1, 2, 3, 4, 5, ... as inputs.)

So this is behaving as expected.

查看更多
三岁会撩人
3楼-- · 2019-03-29 04:22

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

This behavior is defined in the original C standard. I don't have a full copy of it handy, but The Open Group's POSIX standards are the next best thing, as they incorporate the full C standard (with some extensions):

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

The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

The actual result of a rand() call for any given seed is implementation-defined, except that for any seed x, call n to rand() after the seeding of the generator must return the same result. So while on your implementation you will always get 41 on the first rand() call after calling srand(1), and you will always get the same result (whatever it is) for the second rand() call, other implementations may use a different algorithm that produces different results.

查看更多
叛逆
4楼-- · 2019-03-29 04:27

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

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

The C standard (7.20.2.2 of C99) states quite categorically:

If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

So the first time you call rand, the seed is 1. It's also 1 the third time you call it which is why you get the same value.

The second time that you called rand, the seed had been set based on the current time, which is why you got a different result. It's usually not a good idea to re-seed the random number generator unless you actually want the same sequence (such as in testing).

查看更多
Emotional °昔
5楼-- · 2019-03-29 04:31

I think this one may be a problem:

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

srand reference

查看更多
我命由我不由天
6楼-- · 2019-03-29 04:32

It's probably because in your initial call to rand the random number generator has not been seeded so it uses a default value of 1 to seed it.

查看更多
We Are One
7楼-- · 2019-03-29 04:42

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

查看更多
登录 后发表回答