<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.array-rand.php',
    1 => 'array_rand',
    2 => 'Prend une ou plusieurs cl&eacute;s, au hasard dans un tableau',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Fonctions sur les tableaux',
  ),
  'prev' => 
  array (
    0 => 'function.array-push.php',
    1 => 'array_push',
  ),
  'next' => 
  array (
    0 => 'function.array-reduce.php',
    1 => 'array_reduce',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/array/functions/array-rand.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.array-rand" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_rand</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_rand</span> &mdash; <span class="dc-title">Prend une ou plusieurs clés, au hasard dans un tableau</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-rand-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_rand</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$num</code><span class="initializer"> = 1</span></span>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span></span></div>

  <p class="para rdfs-comment">
   Sélectionne une ou plusieurs valeurs au hasard dans un tableau
   et retourne la ou les clés de ces valeurs.
  </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.array-rand-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">array</code></dt>
     <dd>
      <p class="para">
       Le tableau d&#039;entrée.
       Ne peut pas être vide.
      </p>
     </dd>
    
    
     <dt><code class="parameter">num</code></dt>
     <dd>
      <p class="para">
       Spécifie le nombre d&#039;entrées que l&#039;on veut récupérer.
       Doit être supérieur à zéro et inférieur ou égal à la longueur de <code class="parameter">array</code>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-rand-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Lorsque l&#039;on ne récupère qu&#039;une seule entrée, la fonction
   <span class="function"><strong>array_rand()</strong></span> retourne la clé d&#039;une entrée choisie
   aléatoirement. Sinon, un tableau de clés d&#039;entrées aléatoires sera
   retourné. Cela permet de faire une sélection au hasard de clés,
   ou bien de valeurs. Si plusieurs clés sont retournées, alors elles le
   seront dans l&#039;ordre qu&#039;elles étaient dans le tableau d&#039;origine.  
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.array-rand-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <p class="para">
   Lance une <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> si <code class="parameter">array</code> est vide,
   ou si <code class="parameter">num</code> est hors de portée.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-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>
        <span class="function"><strong>array_rand()</strong></span> lance désormais une <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>
        si <code class="parameter">num</code> est hors de portée ; précédemment, un
        <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> était généré et la fonction renvoyait <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        <span class="function"><strong>array_rand()</strong></span> lance désormais une <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>
        si <code class="parameter">array</code> est vide ; précédemment, un
        <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> était généré et la fonction renvoyait <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        L&#039;algorithme 
         interne de génération aléatoire <a href="migration71.incompatible.php#migration71.incompatible.rand-srand-aliases" class="link">a été modifié</a> pour utiliser le 
         générateur aléatoire de nombre <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">&raquo;&nbsp;
         Mersenne Twister</a> au lieu de la fonction aléatoire libc
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-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>array_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 />$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"Neo"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Morpheus"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Trinity"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Cypher"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Tank"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rand_keys </span><span style="color: #007700">= </span><span style="color: #0000BB">array_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$rand_keys</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$rand_keys</span><span style="color: #007700">[</span><span style="color: #0000BB">1</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>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-rand-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="random-randomizer.pickarraykeys.php" class="function" rel="rdfs-seeAlso">Random\Randomizer::pickArrayKeys()</a> - S&eacute;lectionne des cl&eacute;s de tableau al&eacute;atoires</span></li>
    <li><span class="function"><a href="random-randomizer.shufflearray.php" class="function" rel="rdfs-seeAlso">Random\Randomizer::shuffleArray()</a> - Renvoie une permutation d'un tableau</span></li>
   </ul>
  </p>
 </div>

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