random(3) Library Functions Manual random(3) random, srandom, initstate, setstate - C (libc, -lc) #include long random(void); void srandom(unsigned int seed); char *initstate(unsigned int seed, char state[.n], size_t n); char *setstate(char *state); glibc (. feature_test_macros(7)): random(), srandom(), initstate(), setstate(): _XOPEN_SOURCE >= 500 || /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE The random() function uses a nonlinear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to 2^31 - 1. The period of this random number generator is very large, approximately 16 * ((2^31) - 1). srandom() , random(). , srandom() . , random() 1. The initstate() function allows a state array state to be initialized for use by random(). The size of the state array n is used by initstate() to decide how sophisticated a random number generator it should use--the larger the state array, the better the random numbers will be. Current "optimal" values for the size of the state array n are 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to the nearest known amount. Using less than 8 bytes results in an error. seed is the seed for the initialization, which specifies a starting point for the random number sequence, and provides for restarting at the same point. setstate() , random(). state initstate() setstate(). state initstate() setstate(). The random() function returns a value between 0 and (2^31) - 1. The srandom() function returns no value. The initstate() function returns a pointer to the previous state array. On failure, it returns NULL, and errno is set to indicate the error. On success, setstate() returns a pointer to the previous state array. On failure, it returns NULL, and errno is set to indicate the error. EINVAL state, setstate(), NULL. EINVAL initstate() 8 . attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |random(), srandom(), | | MT-Safe | |initstate(), setstate() | | | +----------------------------+----------------------------------------------------------+--------------------------+ POSIX.1-2008. POSIX.1-2001, 4.3BSD. . 7 (Random Numbers) Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.) . , , 3 (Random Numbers) (The Art of Computer Programming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981). CAVEATS random() , . random_r(3). POSIX, initstate() NULL . glibc errno ( ), NULL. getrandom(2), drand48(3), rand(3), random_r(3), srand(3) () aereiae , Azamat Hackimov , Dmitriy S. Seregin , Katrin Kutepova , Lockal , Yuri Kozlov , ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 2 2024 . random(3)