about  srandom( time( NULL ))

来源:互联网 发布:linux 退出cat命令 编辑:程序博客网 时间:2024/05/07 00:09
may I know the meaning or even how toread this: srandom( time( NULL ))?

NULL
A null pointer. Zero. Points tonothing.

time(NULL)
The time function returns the currenttimestamp as an integer. It
accepts an input argument. If theargument is not null, the current time
is stored in it.

srandom(time(NULL))
The s means "seed". srandom means "seedthe random number generator". It
takes an integer as input, reset thePRNG's internal state derived by
the input to generate a sequence ofrandom numbers according to it. The
seed is sometimes used to ensure 2sequences of random numbers are the
same, to reproduce an equivalent testingcondition.

In general, you just put some alwayschanging value there to avoid
having the same sequence every time theprogram is started. The current
timestamp is a good value, so time(NULL)is used as the input.


Name

random, srandom, initstate,setstate - random number generator

Synopsis

#include <stdlib.h>long int random(void);void srandom(unsigned int seed);char *initstate(unsigned int seed, char *state, size_t n);char *setstate(char *state);
FeatureTest Macro Requirements for glibc(see feature_test_macros(7)):
random(), srandom(), initstate(), setstate():
_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >=500 || _XOPEN_SOURCE &&_XOPEN_SOURCE_EXTENDED

Description

The random()function uses a nonlinear additive feedback random number generatoremploying a default table of size 31 long integers to returnsuccessive pseudo-random numbers in the range from 0to RAND_MAX. Theperiod of this random number generator is very large,approximately 16 * ((2^31) - 1).

The srandom()function sets its argument as the seed for a new sequence ofpseudo-random integers to be returned by random(). These sequences are repeatableby calling srandom() withthe same seed value. If no seed value is provided, therandom() function is automatically seededwith a value of 1.

The initstate()function allows a statearray state to beinitialized for use by random(). The size of the statearray n is usedby initstate()to decide how sophisticated a random number generator it shoulduse-the larger the state array, the better the random numbers willbe. seed is the seed forthe initialization, which specifies a starting point for the randomnumber sequence, and provides for restarting at the same point.

The setstate()function changes the state array used bythe random()function. The statearray state is used forrandom number generation until the next callto initstate()or setstate(). state mustfirst have been initialized using initstate() or be the result of a previouscall of setstate().

Return Value

The random()function returns a value between 0 and RAND_MAX. The srandom()function returns no value. The initstate() function returns a pointer tothe previous state array. The setstate() function returns a pointer tothe previous state array, or NULL on error.

Errors

EINVAL
A state array of less than 8 bytes was specifiedto initstate().

Conforming To

4.3BSD, POSIX.1-2001.

Notes

Current "optimal" values for the size of the statearray n are 8, 32, 64,128, and 256 bytes; other amounts will be rounded down to thenearest known amount. Using less than 8 bytes will cause anerror.

This function should not be used in cases where multiple threadsuse random() andthe behavior should be reproducible. Use random_r(3) for thatpurpose.

Random-number generation is a complextopic. Numerical Recipes in C: The Art ofScientific Computing (William H. Press, BrianP. Flannery, Saul A. Teukolsky, William T. Vetterling; New York:Cambridge University Press, 2007, 3rd ed.) provides an excellentdiscussion of practical random-number generation issues in Chapter7 (Random Numbers).

For a more theoretical discussion which also covers many practicalissues in depth, see Chapter 3 (Random Numbers) in Donald E.Knuth's The Art of Computer Programming,volume 2 (Seminumerical Algorithms), 2nd ed.; Reading,Massachusetts: Addison-Wesley Publishing Company, 1981.



原创粉丝点击