<?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.mt-rand.php',
    1 => 'mt_rand',
    2 => 'G&eacute;n&egrave;re une valeur al&eacute;atoire via le g&eacute;n&eacute;rateur de nombre al&eacute;atoire Mersenne Twister',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random Fonctions',
  ),
  'prev' => 
  array (
    0 => 'function.mt-getrandmax.php',
    1 => 'mt_getrandmax',
  ),
  'next' => 
  array (
    0 => 'function.mt-srand.php',
    1 => 'mt_srand',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/random/functions/mt-rand.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

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

 </div>
 <div class="refsect1 description" id="refsect1-function.mt-rand-description">
  <h3 class="title">Description</h3>
   <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mt_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>mt_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">
   De nombreux générateurs de nombres aléatoires
   provenant de vieilles bibliothèques libcs ont des comportements
   douteux et sont très lents. <span class="function"><strong>mt_rand()</strong></span> est une
   fonction de remplacement pour <span class="function"><a href="function.rand.php" class="function">rand()</a></span>. Elle utilise un
   générateur de nombres aléatoire de caractéristique
   connue, le &quot; <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">&raquo;&nbsp;Mersenne Twister</a> &quot; qui
   est 4 fois plus rapide que la fonction standard libc.
  </p>
  <p class="simpara">
   Appelée sans les arguments optionnels <code class="parameter">min</code> et
   <code class="parameter">max</code>, <span class="function"><strong>mt_rand()</strong></span> retourne un nombre
   pseudoaléatoire, entre 0 et <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>.
   Pour obtenir un nombre entre 5 et 15 inclus, il faut utiliser
   <code class="literal">mt_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>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mt-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">
       Valeur la plus basse qui peut être retournée (par défaut : 0)
      </p>
     </dd>
    
    
     <dt><code class="parameter">max</code></dt>
     <dd>
      <p class="para">
       Valeur la plus haute qui peut être retournée (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.mt-rand-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Un <a href="language.types.integer.php" class="link">entier</a> aléatoire compris 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 errors" id="refsect1-function.mt-rand-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Si <code class="parameter">max</code> est inférieur à <code class="parameter">min</code>,
     une exception <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> est levée.
    </span>
   </li>
  </ul>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.mt-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>8.0.0</td>
       <td>
        Une exception <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> est levée si <code class="parameter">max</code> est inférieur à <code class="parameter">min</code> ;
        auparavant, un <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> était émis et la fonction retournait <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><strong>mt_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 avec une valeur d&#039;initialisation spécifique peuvent différer de PHP 7.1 sur les machines 64-bit.
       </td>
      </tr>

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

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><strong>mt_rand()</strong></span>
        <a href="migration71.incompatible.php#migration71.incompatible.fixes-to-mt_rand-algorithm" class="link">a été mis à jour</a>
        pour utiliser la version corrigée, correcte, de l&#039;algorithme Twister
        Mersenne. Pour revenir à l&#039;ancien comportement, utiliser
        <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> avec <strong><code><a href="random.constants.php#constant.mt-rand-php">MT_RAND_PHP</a></code></strong>
        comme deuxième paramètre.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mt-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>mt_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">mt_rand</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">mt_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">mt_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">1604716014
1478613278
6</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mt-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.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>. C.-à-d.
    <code class="parameter">max</code> - <code class="parameter">min</code>) &lt;=
    <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span> sinon, <span class="function"><strong>mt_rand()</strong></span> peut
    retourner des nombres aléatoires plus pauvres qu&#039;il ne le devrait.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mt-rand-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.mt-srand.php" class="function" rel="rdfs-seeAlso">mt_srand()</a> - Initialise le g&eacute;n&eacute;rateur de nombres al&eacute;atoires Mersenne Twister</span></li>
    <li><span class="function"><a href="function.mt-getrandmax.php" class="function" rel="rdfs-seeAlso">mt_getrandmax()</a> - La plus grande valeur al&eacute;atoire possible</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); ?>