Section courante

A propos

Section administrative du site

RAND

Aléatoire
PHP 3+

Syntaxe

function rand();
function 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 à getrandmax() Ces valeurs sont normalement généré.

Description

Cette fonction retourne un nombre aléatoire.

Exemple

Voici un exemple permet d'afficher des nombres aléatoires :

  1. <?php
  2. function randF($minimum = 0, $maximum = 1) {
  3.     return $minimum + (rand() / getrandmax() * ($maximum - $minimum));
  4. }
  5.  
  6. echo "Aleatoire reel (0 a 1) = " . randF() . "<br />";
  7. echo "Aleatoire reel (0 a 9) = " . (randF() * 10) . "<br />";
  8. for($I = 0; $I <= 19; $I++) {
  9.    echo "Aleatoire entier (1 a 100) = " . rand(1,100) . "<br />";
  10. }
  11. ?>

on obtiendra le résultat semblable suivant :

Aleatoire reel (0 a 1) = 0.099899091804353
Aleatoire 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 - getrandmax
Langage de programmation - PHP - Référence procédures et fonctions - mt_rand
Langage de programmation - ASP 3.0 - Référence procédures et fonctions - RND

Dernière mise à jour : Dimanche, le 5 avril 2015