<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.random.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.rand.php',
    1 => 'rand',
    2 => 'G&eacute;n&egrave;re une valeur al&eacute;atoire',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random Fonctions',
  ),
  'prev' => 
  array (
    0 => 'function.mt-srand.php',
    1 => 'mt_srand',
  ),
  'next' => 
  array (
    0 => 'function.random-bytes.php',
    1 => 'random_bytes',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/random/functions/rand.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.rand" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">rand</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">rand</span> &mdash; <span class="dc-title">Génère une valeur aléatoire</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.rand-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>rand</strong></span>(): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <div class="methodsynopsis dc-description"><span class="methodname"><strong>rand</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$min</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$max</code></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="simpara">
   Appelée sans les options <code class="parameter">min</code> et
   <code class="parameter">max</code>, <span class="function"><strong>rand()</strong></span> retourne un
   nombre pseudoaléatoire entre 0 et <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>.
   Pour un nombre aléatoire entre 5 et 15
   (inclus), par exemple, utiliser <code class="literal">rand (5, 15)</code>.
  </p>
  <div class="caution"><strong class="caution">Attention</strong>
 <p class="para">
  Cette fonction ne génère pas de valeurs cryptographiquement sûres, et <em>ne doit pas</em>
  être utilisée à des fins cryptographiques, ou à des fins qui exigent que les valeurs renvoyées soient indéchiffrables.
 </p>
 <p class="para">
  Si de l&#039;aléatoire cryptographiquement sûre est requis, le <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> peut être utilisé
  avec le moteur <span class="classname"><a href="class.random-engine-secure.php" class="classname">Random\Engine\Secure</a></span>. Pour des cas d&#039;usage simple, les fonctions
  <span class="function"><a href="function.random-int.php" class="function">random_int()</a></span> et <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span> fournissent une <abbr title="Application Programming Interface">API</abbr>
   pratique et sûre qui est qui est soutenu par le <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> du système d&#039;exploitation.
 </p>
</div>
  <div class="caution"><strong class="caution">Attention</strong>
 <p class="simpara">
    Cette fonction utilise l&#039;instance globale Mt19937 (&quot;Mersenne Twister&quot;) comme source d&#039;aléatoire et partage ainsi son état avec toutes les autres fonctions utilisant le Mt19937 global.
    L&#039;utilisation de l&#039;une de ces fonctions fait avancer la séquence pour <em>toutes</em> les autres fonctions, indépendamment de la portée.
  </p>
  <p class="simpara">
    Générer des séquences reproductibles en initialisant <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> ou <span class="function"><a href="function.srand.php" class="function">srand()</a></span> avec une valeur connue produira également une sortie reproductible de cette fonction.
  </p>
  <p class="simpara">
    Il est préférable d&#039;utiliser les méthodes de <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> dans tout nouveau code.
 </p>
</div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Avant PHP 7.1.0, <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span> valait seulement 32767 sur certaines plateformes
    (comme Windows). En cas de besoin d’une plage supérieure à 32767, il est recommandé de spécifier
    une valeur limite supérieure à 32767, en spécifiant <code class="parameter">min</code> et
    <code class="parameter">max</code>, l&#039;on sera autorisés à utiliser un intervalle
    plus grand que <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>, ou bien, utiliser la fonction
    <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> à la place.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">À partir de PHP 7.1.0, <span class="function"><strong>rand()</strong></span> utilise le même
   générateur de nombres aléatoires que <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span>. Pour 
   préserver la compatibilité ascendante, <span class="function"><strong>rand()</strong></span> permet à 
   <code class="parameter">max</code> d&#039;être plus petit que <code class="parameter">min</code> 
   par opposition au retour <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> de <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span></span>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.rand-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">min</code></dt>
     <dd>
      <p class="para">
       La plus petite valeur à retourner (par défaut, 0)
      </p>
     </dd>
    
    
     <dt><code class="parameter">max</code></dt>
     <dd>
      <p class="para">
       La plus grande valeur à retourner (par défaut, <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>)
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.rand-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Une valeur pseudoaléatoire, comprise entre
   <code class="parameter">min</code> (ou 0) et
   <code class="parameter">max</code> (ou <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>, inclusif).
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.rand-changelog">
  <h3 class="title">Historique</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><strong>rand()</strong></span> 
        <a href="migration72.incompatible.php#migration72.incompatible.rand-mt_rand-output" class="link">a reçu une correction de bogue</a> 
        pour un bug de polarisation modulo. Cela signifie que les séquences 
        générées dans certains cas spécifiques peuvent différer de PHP 7.1 sur
        les machines 64-bit.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><strong>rand()</strong></span> <a href="migration71.incompatible.php#migration71.incompatible.rand-srand-aliases" class="link">est devenu</a> un alias de <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.rand-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Exemple avec <span class="function"><strong>rand()</strong></span></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">rand</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">rand</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">15</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">7771
22264
11</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.rand-notes">
  <h3 class="title">Notes</h3>
  <div class="warning"><strong class="warning">Avertissement</strong>
   <p class="para">
    La plage <code class="parameter">min</code> <code class="parameter">max</code> doit se situer
    dans la plage <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>. C.-à-d.
    abs(<code class="parameter">max</code> - <code class="parameter">min</code>) &lt;=
    <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span> sinon,
    <span class="function"><strong>rand()</strong></span> peut retourner des nombres aléatoires de mauvaise
    qualité.
   </p>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.rand-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.srand.php" class="function" rel="rdfs-seeAlso">srand()</a> - Initialise le g&eacute;n&eacute;rateur de nombres al&eacute;atoires</span></li>
    <li><span class="function"><a href="function.getrandmax.php" class="function" rel="rdfs-seeAlso">getrandmax()</a> - Plus grande valeur al&eacute;atoire possible</span></li>
    <li><span class="function"><a href="function.mt-rand.php" class="function" rel="rdfs-seeAlso">mt_rand()</a> - G&eacute;n&egrave;re une valeur al&eacute;atoire via le g&eacute;n&eacute;rateur de nombre al&eacute;atoire Mersenne Twister</span></li>
    <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - R&eacute;cup&egrave;re un entier s&eacute;lectionn&eacute; de mani&egrave;re uniforme et cryptographiquement s&eacute;curis&eacute;e</span></li>
    <li><span class="function"><a href="function.random-bytes.php" class="function" rel="rdfs-seeAlso">random_bytes()</a> - R&eacute;cup&egrave;re des octets al&eacute;atoires cryptographiquement s&eacute;curis&eacute;s</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>