Qu'est-ce que le générateur de nombres aléatoires en PHP?

Dans cet article, nous allons découvrir un générateur de nombres aléatoires en PHP. Alors, qu'est-ce qu'un générateur de nombres aléatoires?

Nous pouvons générer des nombres aléatoires ou des entiers en utilisant des fonctions intégrées. Que font ces fonctions? Ces fonctions dans une plage de min et max génèrent différents ensembles de nombres. Et chaque fois que vous appelez cette fonction, elle génère un numéro unique. Nous pouvons générer des chiffres numérotés comme le numéro à 2 chiffres, le numéro à 3 chiffres et ainsi de suite.

Les nombres sont mélangés dans la plage et sont générés en conséquence. Il existe différentes fonctions intégrées pour générer des nombres aléatoires.

Fonctions du générateur de nombres aléatoires

Nous allons maintenant découvrir différentes fonctions qui génèrent des nombres pseudo-aléatoires:

  • fonction rand () sans plage, fonction rand () avec plage: cette fonction, lorsqu'elle est appelée, renvoie un nombre aléatoire. Lorsque les valeurs min et max sont fournies à la fonction, elle génère un nombre aléatoire dans la plage.
  • Fonction mt_rand (): Cette fonction est similaire à rand (). mt dans mt_rand () signifie Mersenne Twister. La fonction mt_rand () est un générateur de nombres aléatoires et renvoie une valeur entière. Il génère un nombre pseudo-aléatoire comme le fait la fonction rand (). C'était le premier générateur de nombres pseudo-aléatoires. Il s'agit d'une forme avancée d'un ancien générateur de nombres aléatoires. Il est rapide, efficace et fournit des entiers de haute qualité.
  • Fonction getrandmax (): Aucun paramètre n'est défini pour cette fonction et, comme son nom l'indique, elle renvoie le nombre aléatoire le plus grand ou le plus grand possible.
  • Fonction mt_getrandmax (): elle est similaire à la fonction getrandmax () et renvoie également le nombre aléatoire le plus grand ou le plus grand possible. Ici encore, mt signifie Mersenne Twister qui est un algorithme pour générer des nombres aléatoires.
  • fonction srand (seed): Cette fonction amorce le générateur de nombres aléatoires avec la valeur d'amorçage donnée si elle ne reçoit pas cette fonction amorce avec un nombre aléatoire
  • mt_srand (seed): Cette fonction est similaire à la fonction srand () et cette fonction amorce le générateur de nombres aléatoires avec la valeur de départ donnée.

Nous allons apprendre la syntaxe suivie des exemples de chaque type de fonction mentionné.

1. Fonction rand ()

Syntaxe:

rand()

Exemple:

<_?php
// program to generate random integer value
echo '
'.'Following are the different random values';
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
?>
// program to generate random integer value
echo '
'.'Following are the different random values';
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
?>
// program to generate random integer value
echo '
'.'Following are the different random values';
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
?>
// program to generate random integer value
echo '
'.'Following are the different random values';
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
echo ' ';
echo '
'. rand();
?>

Production:

2. Fonction rand () dans une plage donnée

Cette fonction fournit la plage de la fonction rand ().

Syntaxe:

rand(min, max);

où min est la valeur minimale facultative et désigne la valeur numérique la plus basse et max est la valeur maximale facultative et la valeur numérique la plus élevée.

En outre, min a une valeur par défaut de zéro et max a une valeur par défaut de la valeur de la fonction getrandmax (). Le type de retour de la fonction est toujours un entier.

Exemple:

<_?php
// program to generate random integer value
echo 'Following are the different random values within ranges min and max';
echo ' ';
echo '
Range : 1 to 100 ----> '. rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. rand(5, 25);
echo ' ';
echo '
Range 10000 to 50000 --->'. rand(10000, 50000);
?>
// program to generate random integer value
echo 'Following are the different random values within ranges min and max';
echo ' ';
echo '
Range : 1 to 100 ----> '. rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. rand(5, 25);
echo ' ';
echo '
Range 10000 to 50000 --->'. rand(10000, 50000);
?>
// program to generate random integer value
echo 'Following are the different random values within ranges min and max';
echo ' ';
echo '
Range : 1 to 100 ----> '. rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. rand(5, 25);
echo ' ';
echo '
Range 10000 to 50000 --->'. rand(10000, 50000);
?>
// program to generate random integer value
echo 'Following are the different random values within ranges min and max';
echo ' ';
echo '
Range : 1 to 100 ----> '. rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. rand(5, 25);
echo ' ';
echo '
Range 10000 to 50000 --->'. rand(10000, 50000);
?>

Production:

3. Fonction mt_rand ()

Syntaxe:

int mt_rand(min, max)

où min est une valeur facultative et désigne le nombre le plus bas et max est une valeur facultative et désigne le nombre le plus élevé. La valeur par défaut de min est 0 et la valeur par défaut de max est la valeur la plus élevée donnée. Le type de retour est un entier.

Exemple:

<_?php
// program to generate random integer value
echo 'Following are the different random values using mt_rand()';
echo ' ';
echo '
Range : 1 to 100 ----> '. mt_rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. mt_rand(5, 25);
echo ' ';
echo '
Range 9 to 19 --->'. mt_rand(9, 19);
?>
// program to generate random integer value
echo 'Following are the different random values using mt_rand()';
echo ' ';
echo '
Range : 1 to 100 ----> '. mt_rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. mt_rand(5, 25);
echo ' ';
echo '
Range 9 to 19 --->'. mt_rand(9, 19);
?>
// program to generate random integer value
echo 'Following are the different random values using mt_rand()';
echo ' ';
echo '
Range : 1 to 100 ----> '. mt_rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. mt_rand(5, 25);
echo ' ';
echo '
Range 9 to 19 --->'. mt_rand(9, 19);
?>
// program to generate random integer value
echo 'Following are the different random values using mt_rand()';
echo ' ';
echo '
Range : 1 to 100 ----> '. mt_rand(1, 100);
echo ' ';
echo '
Range 5 to 25 ---->'. mt_rand(5, 25);
echo ' ';
echo '
Range 9 to 19 --->'. mt_rand(9, 19);
?>

Production:

4. Fonction getrandmax ()

Syntaxe:

mt_getrandmax();

Cette fonction renvoie une valeur entière

Exemple :

<_?php
// program to generate random integer values
//using getrandmax() function
echo 'Random number using getrandmax() function';
echo ' ';
echo(getrandmax());
echo ' ';
?>
// program to generate random integer values
//using getrandmax() function
echo 'Random number using getrandmax() function';
echo ' ';
echo(getrandmax());
echo ' ';
?>
// program to generate random integer values
//using getrandmax() function
echo 'Random number using getrandmax() function';
echo ' ';
echo(getrandmax());
echo ' ';
?>

Production:

5. Fonction mt_getrandommax ()

Syntaxe:

mt_getrandmax();

Cette fonction renvoie une valeur entière.

Exemple:

<_?php
// program to generate random integer values
//using mt_getrandmax() function
echo 'random number using mt_getrandmax() function';
echo ' ';
echo(mt_getrandmax());
?>
// program to generate random integer values
//using mt_getrandmax() function
echo 'random number using mt_getrandmax() function';
echo ' ';
echo(mt_getrandmax());
?>

Production :

6. Fonction srand ()

Syntaxe:

srand(seed);

Où la valeur de départ est une valeur facultative, et cette fonction ne renvoie rien.

Exemple:

<_?php
// program to generate random integer value
echo 'example using srand';
echo '
'. srand(3);
echo(rand(1, 5));
echo ' ';
echo 'example using srand';
echo '
'. srand(2);
echo(rand(1, 5));
?>
// program to generate random integer value
echo 'example using srand';
echo '
'. srand(3);
echo(rand(1, 5));
echo ' ';
echo 'example using srand';
echo '
'. srand(2);
echo(rand(1, 5));
?>

Production:

7. Fonction mt_srand ()

Exemple:

<_?php
// program to generate random integer value using mt_srand() function
echo 'example using mt_srand';
echo ' ';
mt_srand(5);
echo mt_rand(1, 5);
?>
// program to generate random integer value using mt_srand() function
echo 'example using mt_srand';
echo ' ';
mt_srand(5);
echo mt_rand(1, 5);
?>

Production:

Entiers de génération

Dans l'exemple suivant, nous avons utilisé rand (), rand (min, max) et mt_rand ().

Exemple:

<_?php
// program to generate random integer value
echo 'Following are the different random values';
echo '
Any random number ---->'. rand();
echo '
Any random number ---->'. rand();
echo ' ';
// random number with range
echo 'Following are the different random values within a range ';
echo '
Any random number within the range from 0 to 9----> '. rand(0, 9);
echo '
Any random number within the range from 1000 to 9999 ---->'. rand(1000, 9999);
echo ' ';
// random number with range
echo 'Following are the different random values using mt_rand() ';
echo '
Using mt_rand()---->'. mt_rand(1000, 9999);
echo '
Using mt_rand()---->'. mt_rand(100, 999);
?>
// program to generate random integer value
echo 'Following are the different random values';
echo '
Any random number ---->'. rand();
echo '
Any random number ---->'. rand();
echo ' ';
// random number with range
echo 'Following are the different random values within a range ';
echo '
Any random number within the range from 0 to 9----> '. rand(0, 9);
echo '
Any random number within the range from 1000 to 9999 ---->'. rand(1000, 9999);
echo ' ';
// random number with range
echo 'Following are the different random values using mt_rand() ';
echo '
Using mt_rand()---->'. mt_rand(1000, 9999);
echo '
Using mt_rand()---->'. mt_rand(100, 999);
?>
// program to generate random integer value
echo 'Following are the different random values';
echo '
Any random number ---->'. rand();
echo '
Any random number ---->'. rand();
echo ' ';
// random number with range
echo 'Following are the different random values within a range ';
echo '
Any random number within the range from 0 to 9----> '. rand(0, 9);
echo '
Any random number within the range from 1000 to 9999 ---->'. rand(1000, 9999);
echo ' ';
// random number with range
echo 'Following are the different random values using mt_rand() ';
echo '
Using mt_rand()---->'. mt_rand(1000, 9999);
echo '
Using mt_rand()---->'. mt_rand(100, 999);
?>

Production:

Génération de nombres à virgule flottante

Les nombres à virgule flottante représentent un nombre avec des décimales qui sont du type float. Exemples - 10.0, 8.12, 6.23e-5, 2.345, 2.98e + 10 et plus.

Exemple:

<_?php
function fun($min, $max) (
$square_root = sqrt(4);
return mt_rand($min * $square_root, $max * $square_root) / 100;
)
echo 'Program to display floating point numbers ';
echo ' ';
echo "
".fun(1, 10, 2);
?>
function fun($min, $max) (
$square_root = sqrt(4);
return mt_rand($min * $square_root, $max * $square_root) / 100;
)
echo 'Program to display floating point numbers ';
echo ' ';
echo "
".fun(1, 10, 2);
?>

Production:

Conclusion

Dans cet article, nous avons découvert différentes fonctions utilisées pour générer un nombre aléatoire en PHP. Ces fonctions sont expliquées avec des exemples d'exemples. J'espère que cet article sera utile à tous ceux qui veulent apprendre un générateur de nombres aléatoires en PHP.

Articles recommandés

Ceci est un guide du générateur de nombres aléatoires en PHP. Ici, nous discutons des différentes fonctions du générateur de nombres aléatoires en php avec ses exemples. Vous pouvez également consulter les articles suivants pour en savoir plus -

  1. Boucle en PHP (Exemples)
  2. Motifs d'étoiles en PHP
  3. 10 types de tri en PHP
  4. Programmation des sockets en PHP
  5. Guide complet de tri en C # avec des exemples
  6. Fonction de tri en Python avec des exemples
  7. Programmation des sockets en Python