random(3) Library Functions Manual random(3) NAZWA random, srandom, initstate, setstate - generator liczb losowych BIBLIOTEKA Standardowa biblioteka C (libc, -lc) SKLADNIA #include long random(void); void srandom(unsigned int seed); char *initstate(unsigned int seed, char state[.n], size_t n); char *setstate(char *state); Wymagane ustawienia makr biblioteki glibc (patrz feature_test_macros(7)): random(), srandom(), initstate(), setstate(): _XOPEN_SOURCE >= 500 || /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE OPIS 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). Funkcja srandom() ustawia swoj argument jako wartosc poczatkowa dla nowego ciagu pseudolosowych liczb calkowitych zwracanych przez random(). Ciagi te sa powtarzalne przez wywolanie srandom() z ta sama wartoscia poczatkowa. Jesli nie podano wartosci poczatkowej, funkcja random() automatycznie ustawia te wartosc na 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. Funkcja setstate() zmienia tablice stanow wykorzystywana przez funkcje random(). Tablica stanu state jest uzywana do generacji liczb losowych az do nastepnego wywolania initstate() lub setstate(). state musi byc uprzednio zainicjowana za pomoca initstate() lub byc wynikiem wczesniejszego wywolania setstate(). WARTOSC ZWRACANA 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. BLEDY EINVAL Argument state przekazany do setstate() byl rowny NULL. EINVAL Podano mniejsza niz 8-bajtowa tablice stanow dla initstate(). ATRYBUTY Informacje o pojeciach uzywanych w tym rozdziale mozna znalezc w podreczniku attributes(7). +---------------------------+--------------------------+---------------+ |Interfejs | Atrybut | Wartosc | +---------------------------+--------------------------+---------------+ |random(), srandom(), | Bezpieczenstwo watkowe | MT-bezpieczne | |initstate(), setstate() | | | +---------------------------+--------------------------+---------------+ STANDARDY POSIX.1-2008. HISTORIA POSIX.1-2001, 4.3BSD. UWAGI Generowanie liczb pseudolosowych jest zlozonym tematem. "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, wydanie 3.) w rozdziale 7 ("Random Numbers") zawiera wybitna dyskusje o praktycznych problemach generowania liczb pseudolosowych. Bardziej teoretyczna dyskusje, takze zawierajaca szczegolowy opis wielu praktycznych problemow mozna znalezc w rozdziale 3. ("Random Numbers") drugiego tomu ("Seminumerical Algorithms") ksiazki Donalda E. Knutha "The Art of Computer Programming"; wydanie drugie; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981. CAVEATS Funkcja random() nie powinna byc uzywana w programach wielowatkowych, gdy wymagane jest, by jej zachowanie bylo powtarzalne. Do tego celu nalezy uzyc random_r(3). USTERKI Zgodnie z POSIX funkcja initstate() powinna zwrocic NULL w przypadku bledu. W implementacji glibc w razie bledu ustawiane jest errno (jak okreslono w POSIX), ale funkcja nie zwraca NULL. ZOBACZ TAKZE getrandom(2), drand48(3), rand(3), random_r(3), srand(3) TLUMACZENIE Autorami polskiego tlumaczenia niniejszej strony podrecznika sa: Andrzej Krzysztofowicz , Robert Luberda i Michal Kulach Niniejsze tlumaczenie jest wolna dokumentacja. Blizsze informacje o warunkach licencji mozna uzyskac zapoznajac sie z GNU General Public License w wersji 3 lub nowszej. Nie przyjmuje sie ZADNEJ ODPOWIEDZIALNOSCI. Bledy w tlumaczeniu strony podrecznika prosimy zglaszac na adres listy dyskusyjnej . Linux man-pages 6.06 31 pazdziernika 2023 r. random(3)