Fonction PHP mt_rand()
Exemple
Générer des nombres aléatoires :
<?php
echo(mt_rand() . "<br>");
echo(mt_rand() . "<br>");
echo(mt_rand(10,100));
?>
Définition et utilisation
La fonction mt_rand() génère un entier aléatoire à l'aide de l'algorithme Mersenne Twister.
Exemple d'astuce : si vous voulez un entier aléatoire entre 10 et 100 (inclus), utilisez mt_rand (10,100).
Syntaxe
mt_rand();
or
mt_rand(min,max);
Valeurs des paramètres
Parameter | Description |
---|---|
min | Optional. Specifies the lowest number to return. Default is 0 |
max | Optional. Specifies the highest number to return. Default is mt_getrandmax() |
Détails techniques
Valeur de retour : | Un entier aléatoire entre min (ou 0) et max (ou mt_getrandmax() inclus). Renvoie FALSE si max < min |
---|---|
Type de retour : | Entier |
Version PHP : | 4+ |
Journal des modifications PHP : | PHP 7.1 : rand() était un alias de mt_rand() PHP 5.3.4 : émet un E_WARNING et renvoie FALSE si max < min . PHP 4.2.0 : Le générateur de nombres aléatoires est automatiquement initialisé. |
❮ Référence mathématique PHP