Scroll to navigation

rand(3) Library Functions Manual rand(3)

NOM

rand, rand_r, srand - Gnrateur de nombres pseudoalatoires

BIBLIOTHÈQUE

Bibliothèque C standard (libc, -lc)

SYNOPSIS

#include <stdlib.h>
int rand(void);
void srand(unsigned int graine);
[[deprecated]] int rand_r(unsigned int *seedp);

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

rand_r():


Depuis la glibc 2.24 :
_POSIX_C_SOURCE >= 199506L
glibc 2.23 et antrieures
_POSIX_C_SOURCE

DESCRIPTION

La fonction rand() renvoie un entier pseudoalatoire entre 0 et RAND_MAX, bornes incluses (c'est--dire dans l'intervalle mathmatique [0,\fBRAND_MAX]).

La fonction srand() utilise son argument comme  graine  pour la gnration d'une nouvelle squence de nombres pseudoalatoires qui seront fournis par rand(). Ces squences sont reproductibles en appelant srand() avec la mme valeur de graine.

Si aucune graine originale n'est fournie, la fonction rand() commence en utilisant la valeur 1.

La fonction rand() n'est pas rentrante, car elle utilise un tat cach modifi chaque appel. Il peut s'agir simplement de la valeur de graine alatoire pour l'appel suivant ou de quelque chose de plus compliqu. Afin d'obtenir un comportement reproductible dans une application threade, cet tat doit tre explicite; cela peut tre fait en utilisant la fonction rentrante rand_r().

Comme rand(), rand_r() renvoie un entier pseudoalatoire dans l'intervalle [0, RAND_MAX]. L'argument seedp est un pointeur vers un unsigned int qui est utilis pour stocker l'tat entre des appels successifs. Si la fonction rand_r() est appele avec la mme valeur initiale pour l'entier point par seedp, et que cette valeur n'est pas modifie entre les appels, alors la mme squence pseudoalatoire sera gnre.

La valeur pointe par l'argument seedp de rand_r() ne fournit qu'une donne trs petite pour stocker la valeur d'tat, cette fonction sera donc un gnrateur pseudoalatoire faible. Essayez donc drand48_r(3) sa place.

VALEUR RENVOYÉE

Les fonctions rand() et rand_r() renvoient un nombre entier entre 0 et RAND_MAX, bornes incluses. La fonction srand() ne renvoie aucune valeur.

ATTRIBUTS

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

Interface Attribut Valeur
rand(), rand_r(), srand() Sécurité des threads MT-Safe

VERSIONS

Les versions de rand() et srand() de la bibliothqueC de Linux utilisent le mme gnrateur de nombres alatoires que random(3) et srandom(3), ainsi les bits de poids faible sont tout aussi imprvisibles que les bits de poids fort. Ceci n'est pas le cas avec les anciennes implmentations de rand() ou d'actuelles implmentations sur des systmes diffrents, o les bits de poids faible n'taient pas  aussi alatoires  que ceux de poids fort. N'utilisez pas cette fonction dans des applications conues pour tre portables et lorsqu'un bon caractre alatoire est ncessaire. (Utilisez plutt random(3))

STANDARDS

C11, POSIX.1-2008.
POSIX.1-2008.

HISTORIQUE

SVr4, 4.3BSD, C89, POSIX.1-2001.
POSIX.1-2001. Obsolete in POSIX.1-2008.

EXEMPLES

POSIX.1-2001 fournit l'exemple suivant d'une implmentation de rand() et srand() potentiellement utile lorsqu'on a besoin de la mme squence sur deux machines diffrentes.


static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {

next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768); } void mysrand(unsigned int seed) {
next = seed; }

Le programme suivant peut tre utilis pour afficher la squence pseudoalatoire produite par rand() avec une graine donne. Quand la graine est -1, le programme utilise une graine alatoire.


#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{

int r;
unsigned int seed, nloops;
if (argc != 3) {
fprintf(stderr, "Usage: %s <seed> <nloops>\n", argv[0]);
exit(EXIT_FAILURE);
}
seed = atoi(argv[1]);
nloops = atoi(argv[2]);
if (seed == -1) {
seed = arc4random();
printf("seed: %u\n", seed);
}
srand(seed);
for (unsigned int j = 0; j < nloops; j++) {
r = rand();
printf("%d\n", r);
}
exit(EXIT_SUCCESS); }

VOIR AUSSI

drand48(3), random(3)

TRADUCTION

La traduction française de cette page de manuel a été créée par #-#-#-#-# min-002-occurences.po (perkamon) #-#-#-#-#, Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Jean-Philippe MENGUAL <jpmengual@debian.org>, Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>, #-#-#-#-# min-003-occurences.po (perkamon) #-#-#-#-#, Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Jean-Philippe MENGUAL <jpmengual@debian.org>, Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>, #-#-#-#-# min-004-occurences.po (perkamon) #-#-#-#-#, Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Jean-Philippe MENGUAL <jpmengual@debian.org>, Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>, #-#-#-#-# min-010-occurences.po (perkamon) #-#-#-#-#, Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Thomas Vincent <tvincent@debian.org>, #-#-#-#-# min-020-occurences.po (perkamon) #-#-#-#-#, Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Jean-Philippe MENGUAL <jpmengual@debian.org>, Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>, #-#-#-#-# min-100-occurences.po (perkamon) #-#-#-#-#, Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Jean-Philippe MENGUAL <jpmengual@debian.org>, Jean-Pierre Giraud <jean-pierregiraud@neuf.fr> et #

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@lists.debian.org.

20 juillet 2023 Pages du manuel de Linux 6.05.01