Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Génère un nombre pseudo-aléatoire. Une version plus sécurisée par programmation de cette fonction est disponible ; voir rand_s. Les nombres générés par rand ne sont pas sécurisés par chiffrement. Pour une génération de nombres aléatoires plus sécurisée par chiffrement, utilisez rand_s ou les fonctions déclarées dans la bibliothèque standard C++ dans <random>.
Syntaxe
int rand(void);
Valeur retournée
rand retourne un nombre pseudo-aléatoire, comme décrit ci-dessus. Il n’existe aucun retour d’erreur.
Notes
La fonction rand retourne un entier pseudo-aléatoire compris entre 0 et RAND_MAX (32 767). Utilisez la srand fonction pour amorçage du générateur pseudorandom-number avant d’appeler rand.
La rand fonction génère une séquence connue et n’est pas appropriée pour être utilisée comme fonction de chiffrement. Pour une génération de nombres aléatoires plus sécurisée par chiffrement, utilisez rand_s ou les fonctions déclarées dans la bibliothèque standard C++ dans <random>.
Par défaut, l’état global de cette fonction est limité à l’application. Pour modifier ce comportement, consultez État global dans le CRT.
Spécifications
| Routine | En-tête requis |
|---|---|
rand |
<stdlib.h> |
Pour plus d’informations sur la compatibilité, consultez Compatibility.
Exemple
// crt_rand.c
// This program seeds the random-number generator
// with a fixed seed, then exercises the rand function
// to demonstrate generating random numbers, and
// random numbers in a specified range.
#include <stdlib.h> // rand(), srand()
#include <stdio.h> // printf()
void SimpleRandDemo(int n)
{
// Print n random numbers.
for (int i = 0; i < n; i++)
{
printf(" %6d\n", rand());
}
}
void RangedRandDemo(int range_min, int range_max, int n)
{
// Generate random numbers in the interval [range_min, range_max], inclusive.
for (int i = 0; i < n; i++)
{
// Note: This method of generating random numbers in a range isn't suitable for
// applications that require high quality random numbers.
// rand() has a small output range [0,32767], making it unsuitable for
// generating random numbers across a large range using the method below.
// The approach below also may result in a non-uniform distribution.
// More robust random number functionality is available in the C++ <random> header.
// See https://learn.microsoft.com/cpp/standard-library/random
int r = ((double)rand() / RAND_MAX) * (range_max - range_min) + range_min;
printf(" %6d\n", r);
}
}
int main(void)
{
// Seed the random-number generator with a fixed seed so that
// the numbers will be the same every time we run.
srand(1792);
printf("Simple random number demo ====\n\n");
SimpleRandDemo(10);
printf("\nRandom number in a range demo ====\n\n");
RangedRandDemo(-100, 100, 100000);
}```
```Output
Simple random number demo ====
5890
1279
19497
1207
11420
3377
15317
29489
9716
23323
Random number in a range demo ====
-82
-46
50
77
-47
32
76
-13
-58
90
Voir aussi
Prise en charge des fonctions mathématiques et à virgule flottante
srand
rand_s
Bibliothèque C++ <random>