MT_RAND |
Aléatoire |
|---|---|
| PHP 4+ | |
Syntaxe
| function mt_rand(); |
| function mt_rand($min,$max); |
Paramètres
| Nom | Description |
|---|---|
| $min | Ce paramètre permet d'indiquer la valeur minimum |
| $max | Ce paramètre permet d'indiquer la valeur maximum |
Retour
| Valeur | Description |
|---|---|
| 0 à mt_getrandmax() | Ces valeurs sont normalement généré. |
Description
Cette fonction permet de retourner un nombre aléatoire en utilisant l'algorithme «Mersenne Twister».
Exemple
Voici un exemple permet d'afficher des nombres aléatoires :
- <?php
- function randF($minimum = 0, $maximum = 1) {
- return $minimum + (mt_rand() / mt_getrandmax() * ($maximum - $minimum));
- }
-
- echo "Aleatoire reel (0 a 1) = " . randF() . "<br />";
- echo "Aleatoire reel (0 a 9) = " . (randF() * 10) . "<br />";
- for($I = 0; $I <= 19; $I++) {
- echo "Aleatoire entier (1 a 100) = " . mt_rand(1,100) . "<br />";
- }
- ?>
on obtiendra le résultat semblable suivant :
Aleatoire reel (0 a 1) = 0.099899091804353Aleatoire reel (0 a 9) = 8.7693073920763
Aleatoire entier (1 a 100) = 73
Aleatoire entier (1 a 100) = 60
Aleatoire entier (1 a 100) = 88
Aleatoire entier (1 a 100) = 1
Aleatoire entier (1 a 100) = 48
Aleatoire entier (1 a 100) = 52
Aleatoire entier (1 a 100) = 92
Aleatoire entier (1 a 100) = 99
Aleatoire entier (1 a 100) = 44
Aleatoire entier (1 a 100) = 22
Aleatoire entier (1 a 100) = 90
Aleatoire entier (1 a 100) = 78
Aleatoire entier (1 a 100) = 46
Aleatoire entier (1 a 100) = 67
Aleatoire entier (1 a 100) = 41
Aleatoire entier (1 a 100) = 13
Aleatoire entier (1 a 100) = 87
Aleatoire entier (1 a 100) = 34
Aleatoire entier (1 a 100) = 55
Aleatoire entier (1 a 100) = 23
Voir également
Langage de programmation - PHP - Référence procédures et fonctions - rand
Langage de programmation - ASP 3.0 - Référence procédures et fonctions - RND
Dernière mise à jour : Dimanche, le 5 avril 2015