sourCEntral - mobile manpages

pdf

random

NOM

random, srandom, initstate, setstate - Générateur de nombres aléatoires

BIBLIOTHÈQUE

Bibliothèque C standard (libc, -lc)

SYNOPSIS

#include <stdlib.h>

long random(void);
void srandom(unsigned int
graine);

char *initstate(unsigned int seed, char state[.n], size_t n);
char *setstate(char *
state);

Exigences de macros de test de fonctionnalités pour la glibc (consulter feature_test_macros(7)) :

random(), srandom(), initstate(), setstate() :
_XOPEN_SOURCE >= 500
|| /* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE

DESCRIPTION

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).

La fonction srandom() utilise son argument comme « graine » pour engendrer une nouvelle séquence de nombres pseudoaléatoires qui seront fournis lors des appels à random(). Ces séquences sont reproductibles en invoquant srandom() avec la même graine. Si aucune graine n’est fournie, La fonction random() utilise automatiquement une graine originale de valeur 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.

La fonction setstate() modifie la table d’états utilisée par la fonction random(). La table d’états état est alors utilisée comme générateur de nombres aléatoires jusqu’au prochain appel de initstate() ou setstate(). état doit d’abord être initialisée avec initstate() ou être le résultat d’un appel précédent à setstate().

VALEUR RENVOYÉE

The random() function returns a value between 0 and (2^31) - 1. The srandom() function returns no value.

La fonction initstate() renvoie un pointeur sur la table d’états précédente. En cas d’échec, elle renvoie NULL et errno contient le code d’erreur.

La fonction setstate() renvoie un pointeur sur la table d’états précédente. En cas d’échec, NULL est renvoyé et errno contient le code d’erreur.

ERREURS

EINVAL

Le paramètre état de setstate() était NULL.

EINVAL

Une table d’états de moins de 8 octets a été fournie à initstate().

ATTRIBUTS

Pour une explication des termes utilisés dans cette section, consulter attributes(7).

img

STANDARDS

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

NOTES

La fonction random() ne doit pas être utilisée dans des programmes multithreadés où le comportement doit être reproductible. Utilisez random_r(3) dans ce cas.

La génération de nombres aléatoires est un sujet complexe. 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, 3e éd.) fournit une excellente discussion sur les problèmes pratiques de génération de noms aléatoires dans le chapitre 7 (Random Numbers).

Pour une discussion plus théorique, qui aborde également en profondeur d’autres domaines, voir le chapitre 3 (Random Numbers) du livre de Donald E. Knuth The Art of Computer Programming, volume 2 (Seminumerical Algorithms), 2e éd. ; Reading, Massachusetts : Addison-Wesley Publishing Company, 1981.

BOGUES

D’après POSIX, initstate() devrait renvoyer NULL en cas d’erreur. Dans la mise en œuvre de la glibc, errno est renseigné en cas d’erreur comme spécifié, mais la fonction ne renvoie pas NULL.

VOIR AUSSI

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

TRADUCTION

La traduction française de cette page de manuel a été créée par Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan DOT rafin AT laposte DOT net>, Thierry Vignaud <tvignaud AT mandriva DOT com>, François Micaux, Alain Portal <aportal AT univ-montp2 DOT fr>, Jean-Philippe Guérard <fevrier AT tigreraye DOT org>, Jean-Luc Coulon (f5ibh) <jean-luc DOT coulon AT wanadoo DOT fr>, Julien Cristau <jcristau AT debian DOT org>, Thomas Huriaux <thomas DOT huriaux AT gmail DOT com>, Nicolas François <nicolas DOT francois AT centraliens DOT net>, Florentin Duneau <fduneau AT gmail DOT com>, Simon Paillard <simon DOT paillard AT resel DOT enst-bretagne DOT fr>, Denis Barbier <barbier AT debian DOT org>, David Prévot <david AT tilapin DOT org> et bubu <bubub AT no-log DOT org>

Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n’y a aucune RESPONSABILITÉ LÉGALE.

Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french.

pdf