<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.rand.php',
    1 => 'rand',
    2 => 'Generate a random integer',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random Functions',
  ),
  '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' => 'en',
    '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">Generate a random integer</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">
   If called without the optional <code class="parameter">min</code>,
   <code class="parameter">max</code> arguments <span class="function"><strong>rand()</strong></span>
   returns a pseudo-random integer between 0 and
   <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>.  If you want a random number
   between 5 and 15 (inclusive), for example, use <code class="literal">rand(5,
   15)</code>.
  </p>
  <div class="caution"><strong class="caution">Caution</strong>
 <p class="para">
  This function does not generate cryptographically secure values, and <em>must not</em>
  be used for cryptographic purposes, or purposes that require returned values to be unguessable.
 </p>
 <p class="para">
  If cryptographically secure randomness is required, the <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> may be
  used with the <span class="classname"><a href="class.random-engine-secure.php" class="classname">Random\Engine\Secure</a></span> engine. For simple use cases, the <span class="function"><a href="function.random-int.php" class="function">random_int()</a></span>
  and <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span> functions provide a convenient and secure <abbr title="Application Programming Interface">API</abbr> that is backed by
  the operating system’s <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr>.
 </p>
</div>
  <div class="caution"><strong class="caution">Caution</strong>
 <p class="simpara">
    This function uses the global Mt19937 (“Mersenne Twister”) instance as the source of randomness and thus shares its state with all other functions using the global Mt19937.
    Using any of these functions advances the sequence for <em>all</em> the other functions, regardless of scope.
  </p>
  <p class="simpara">
    Generating repeatable sequences by seeding <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> or <span class="function"><a href="function.srand.php" class="function">srand()</a></span> with a known value will also yield repeatable output from this function.
  </p>
  <p class="simpara">
    Prefer using <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> methods in all newly written code.
 </p>
</div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Prior to PHP 7.1.0, <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span> was only 32767 on some
    platforms (such as Windows). If you require a range larger than 32767, specifying
    <code class="parameter">min</code> and <code class="parameter">max</code> will allow
    you to create a range larger than this, or consider using
    <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> instead.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">As of PHP 7.1.0, <span class="function"><strong>rand()</strong></span> uses the same
   random number generator as <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span>. To preserve
   backwards compatibility <span class="function"><strong>rand()</strong></span> allows
   <code class="parameter">max</code> to be smaller than <code class="parameter">min</code>
   as opposed to returning <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> as <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">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">min</code></dt>
     <dd>
      <p class="para">
       The lowest value to return (default: 0)
      </p>
     </dd>
    
    
     <dt><code class="parameter">max</code></dt>
     <dd>
      <p class="para">
       The highest value to return (default: <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>)
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.rand-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   A pseudo random value between <code class="parameter">min</code>
   (or 0) and <code class="parameter">max</code> (or <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>, inclusive).
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.rand-changelog">
  <h3 class="title">Changelog</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">has received a bug fix</a> for a modulo bias bug. This means that sequences generated with a specific seed may differ from PHP 7.1 on 64-bit machines.
       </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">has been made</a> an alias of <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">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>rand()</strong></span> example</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>The above example will output
something similar to:</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">Warning</strong>
   <p class="para">
    <code class="parameter">min</code> <code class="parameter">max</code> range must
    be within the range <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>. i.e.
    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>.
    Otherwise, <span class="function"><strong>rand()</strong></span> may return poor-quality random numbers.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.rand-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.srand.php" class="function" rel="rdfs-seeAlso">srand()</a> - Seed the random number generator</span></li>
    <li><span class="function"><a href="function.getrandmax.php" class="function" rel="rdfs-seeAlso">getrandmax()</a> - Show largest possible random value</span></li>
    <li><span class="function"><a href="function.mt-rand.php" class="function" rel="rdfs-seeAlso">mt_rand()</a> - Generate a random value via the Mersenne Twister Random Number Generator</span></li>
    <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - Get a cryptographically secure, uniformly selected integer</span></li>
    <li><span class="function"><a href="function.random-bytes.php" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Get cryptographically secure random bytes</span></li>
   </ul>
  </p>
 </div>

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