Scroll to navigation

random(3) Library Functions Manual random(3)

NAZWA

random, srandom, initstate, setstate - generator liczb losowych

BIBLIOTEKA

Standardowa biblioteka C (libc, -lc)

SKŁADNIA

#include <stdlib.h>
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 swój argument jako wartość początkową dla nowego ciągu pseudolosowych liczb całkowitych zwracanych przez random(). Ciągi te są powtarzalne przez wywołanie srandom() z tą samą wartością początkową. Jeśli nie podano wartości początkowej, funkcja random() automatycznie ustawia tę wartość 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 tablicę stanów wykorzystywaną przez funkcję random(). Tablica stanu state jest używana do generacji liczb losowych aż do następnego wywołania initstate() lub setstate(). state musi być uprzednio zainicjowana za pomocą initstate() lub być wynikiem wcześniejszego wywołania setstate().

WARTOŚĆ 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.

BŁĘDY

Argument state przekazany do setstate() był równy NULL.
Podano mniejszą niż 8-bajtową tablicę stanów dla initstate().

ATRYBUTY

Informacje o pojęciach używanych w tym rozdziale można znaleźć w podręczniku attributes(7).

Interfejs Atrybut Wartość
random(), srandom(), initstate(), setstate() Bezpieczeństwo wątkowe MT-Safe

STANDARDY

POSIX.1-2001, POSIX.1-2008, 4.3BSD.

UWAGI

Funkcja random() nie powinna być używana w programach wielowątkowych, gdy wymagane jest, by jej zachowanie było powtarzalne. Do tego celu należy użyć random_r(3).

Generowanie liczb pseudolosowych jest złożonym 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 wybitną dyskusję o praktycznych problemach generowania liczb pseudolosowych.

Bardziej teoretyczną dyskusję, także zawierającą szczegółowy opis wielu praktycznych problemów można znaleźć w rozdziale 3. ("Random Numbers") drugiego tomu ("Seminumerical Algorithms") książki Donalda E. Knutha "The Art of Computer Programming"; wydanie drugie; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981.

BŁĘDY

Zgodnie z POSIX funkcja initstate() powinna zwrócić NULL w przypadku błędu. W implementacji glibc w razie błędu ustawiane jest errno (jak określono w POSIX), ale funkcja nie zwraca NULL.

ZOBACZ TAKŻE

getrandom(2), drand48(3), rand(3), random_r(3), srand(3)

TŁUMACZENIE

Autorami polskiego tłumaczenia niniejszej strony podręcznika są: Andrzej Krzysztofowicz <ankry@green.mf.pg.gda.pl>, Robert Luberda <robert@debian.org> i Michał Kułach <michal.kulach@gmail.com>

Niniejsze tłumaczenie jest wolną dokumentacją. Bliższe informacje o warunkach licencji można uzyskać zapoznając się z GNU General Public License w wersji 3 lub nowszej. Nie przyjmuje się ŻADNEJ ODPOWIEDZIALNOŚCI.

Błędy w tłumaczeniu strony podręcznika prosimy zgłaszać na adres listy dyskusyjnej manpages-pl-list@lists.sourceforge.net.

10 lutego 2023 r. Linux man-pages 6.03