srand(time(0)) and random number generation

2020-02-04 14:23发布

问题:

srand(time(0)) 

is used in C++ to help in the generation of random numbers by seeding rand with a starting value.

But, can you explain what it exactly does?

Thanks.

回答1:

srand() gives the random function a new seed, a starting point (usually random numbers are calculated by taking the previous number (or the seed) and then do many operations on that number to generate the next).

time(0) gives the time in seconds since the Unix epoch, which is a pretty good "unpredictable" seed (you're guaranteed your seed will be the same only once, unless you start your program multiple times within the same second).



回答2:

From what I remember, there is an equation that is used to generate a sequence of values. The next value is somewhat influenced by the previous value. By using the time, you are setting the initial value of the equation. Keep in mind, the values are pseudo random.

So for example, if you do something like this:


srand(1);
srand(1);

The same sequence of numbers will be generated. However, if you do:


srand(time(0));
srand(time(0) + 1);

Two different sequences of numbers will b e generated because the seed value is different.



回答3:

The C rand function generates random numbers using a seed (just like most -- any? -- pseudo-random generator).

srand is used to set the seed to be used by the random generator algorithm. The value returned by time (ie: the current second) is usually passed to such function because it's the most simple-to-get value that is usually different between two executions of an application



回答4:

firstly, srand & rand is for/from C, not C++, C++0x is introducing its own random number generator classes.

Other than that srand & rand are implementation defined, they only need deliver a random between 0 and RAND_MAX. On windows its a basic MWC16/32, that uses TLS to store seeds, srand sets that seed, which is then used by rand to roll a pseudo random number.

By way of random number generators its pretty terrible, especially the windows version.



回答5:

There's an article at Wikipedia that gives some good history and examples of algorithms used.

The short version is that rand() and its ilk are pseudorandom number generators. In fact, they're actually deterministic—the sequence of numbers produced is always the same. However, the sequence is very very long, and the seeding mechanism provides a means of starting at a (more or less) arbitrary place in that sequence.

Depending on how the random numbers will be used, there are a variety of criteria for evaluating the quality of a pseudorandom generator. In very simple circumstances, perhaps all that's needed is a low probability of repeated values from consecutive calls to rand(). However, you're likely to want the numbers to conform to a particular statistical distribution as well. (As far as I know, most PRNGs produce uniformly distributed numbers. However various functions exist or can be easily written to transform this into a gaussian distribution or any other you might need.)

Finally, when security is a concern, you want an algorithm that is, for all practical purposes, unpredictable. If an attacker knows the previously generated number, she shouldn't be able to then anticipate the next. In very high security applications, a truly random number generator might be used; these would rely on various external sources of randomness, such as radio static, the thermal noise in an image capture device (lens cap on), or other such sources. Also, many modern operating systems (including Linux) gather and store an "entropy pool" from various sources such as unpredictable user interactions and allow the production of random numbers from that.

A common programming error from people not used to working with pseudorandom generators is to re-seed before every call to rand(). Of course, this is unnecessary and probably undesirable. Seed each generator only once.



回答6:

#include "stdio.h" //rmv coding for srand is pure
#include "conio.h"
#include "time.h"

void main()
{

    time_t t;

    srand(time(0));

    for (i = 1; i <= 10; i++) 
        cout << (unsigned) rand() % 1000 - 90 << "\t";

    for (i = 1; i <= 10; i++)
        cout << (char) rand() % 100 - 90 << "\t";

    getch();

}


标签: c++ random