<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Generate a better random value</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mt-getrandmax.html">mt_getrandmax</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mt-srand.html">mt_srand</a></div> <div class="up"><a href="ref.math.html">Math Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.mt-rand" class="refentry"> <div class="refnamediv"> <h1 class="refname">mt_rand</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mt_rand</span> — <span class="dc-title">Generate a better random value</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="type">int</span> <span class="methodname"><strong>mt_rand</strong></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>mt_rand</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$min</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$max</code></span> )</div> <p class="simpara"> Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the <span class="function"><a href="function.rand.html" class="function">rand()</a></span> function. The <span class="function"><strong>mt_rand()</strong></span> function is a drop-in replacement for this. It uses a random number generator with known characteristics using the <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">» Mersenne Twister</a>, which will produce random numbers four times faster than what the average libc rand() provides. </p> <p class="simpara"> If called without the optional <em><code class="parameter">min</code></em>, <em><code class="parameter">max</code></em> arguments <span class="function"><strong>mt_rand()</strong></span> returns a pseudo-random value between 0 and <span class="function"><a href="function.mt-getrandmax.html" class="function">mt_getrandmax()</a></span>. If you want a random number between 5 and 15 (inclusive), for example, use <em>mt_rand(5, 15)</em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.mt-rand-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">min</code></em></span> <dd> <p class="para"> Optional lowest value to be returned (default: 0) </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">max</code></em></span> <dd> <p class="para"> Optional highest value to be returned (default: <span class="function"><a href="function.mt-getrandmax.html" class="function">mt_getrandmax()</a></span>) </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.mt-rand-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A random integer value between <em><code class="parameter">min</code></em> (or 0) and <em><code class="parameter">max</code></em> (or <span class="function"><a href="function.mt-getrandmax.html" class="function">mt_getrandmax()</a></span>, inclusive), or <strong><code>FALSE</code></strong> if <em><code class="parameter">max</code></em> is less than <em><code class="parameter">min</code></em>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.mt-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>5.3.4</td> <td> Issues an <strong><code>E_WARNING</code></strong> and returns <strong><code>FALSE</code></strong> if <em><code class="parameter">max</code></em> < <em><code class="parameter">min</code></em>. </td> </tr> <tr><td>4.2.0</td><td>The random number generator is seeded automatically.</td></tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.mt-rand-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3467"> <p><strong>Example #1 <span class="function"><strong>mt_rand()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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">);<br /></span><span style="color: #0000BB">?></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="cdata"><pre> 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="caution"><strong class="caution">Caution</strong><p class="para">This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using <span class="function"><a href="function.openssl-random-pseudo-bytes.html" class="function">openssl_random_pseudo_bytes()</a></span> instead.</p></div> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> The distribution of <span class="function"><strong>mt_rand()</strong></span> return values is biased towards even numbers on 64-bit builds of PHP when <em><code class="parameter">max</code></em> is beyond <em>2^32</em>. </p> </div> </div> <div class="refsect1 seealso" id="refsect1-function.mt-rand-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.mt-srand.html" class="function" rel="rdfs-seeAlso">mt_srand()</a> - Seed the better random number generator</span></li> <li class="member"> <span class="function"><a href="function.mt-getrandmax.html" class="function" rel="rdfs-seeAlso">mt_getrandmax()</a> - Show largest possible random value</span></li> <li class="member"> <span class="function"><a href="function.openssl-random-pseudo-bytes.html" class="function" rel="rdfs-seeAlso">openssl_random_pseudo_bytes()</a> - Generate a pseudo-random string of bytes</span></li> <li class="member"> <span class="function"><a href="function.rand.html" class="function" rel="rdfs-seeAlso">rand()</a> - Generate a random integer</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mt-getrandmax.html">mt_getrandmax</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mt-srand.html">mt_srand</a></div> <div class="up"><a href="ref.math.html">Math Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>