<!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>Generates cryptographically secure pseudo-random integers</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.random-bytes.html">random_bytes</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.hash.html">Hash</a></div> <div class="up"><a href="ref.csprng.html">CSPRNG Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.random-int" class="refentry"> <div class="refnamediv"> <h1 class="refname">random_int</h1> <p class="verinfo">(PHP 7)</p><p class="refpurpose"><span class="refname">random_int</span> — <span class="dc-title">Generates cryptographically secure pseudo-random integers</span></p> </div> <div class="refsect1 description" id="refsect1-function.random-int-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>random_int</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="para rdfs-comment"> Generates cryptographic random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game. </p> <p class="para"> The sources of randomness used for this function are as follows: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> On Windows, <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx" class="link external">» <span class="function"><strong>CryptGenRandom()</strong></span></a> will always be used. </span> </li> <li class="listitem"> <span class="simpara"> On Linux, the <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">» getrandom(2)</a> syscall will be used if available. </span> </li> <li class="listitem"> <span class="simpara"> On other platforms, <var class="filename">/dev/urandom</var> will be used. </span> </li> <li class="listitem"> <span class="simpara"> If none of the aforementioned sources are available, then an <a href="class.exception.html" class="classname">Exception</a> will be thrown. </span> </li> </ul> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Although this function was added to PHP in PHP 7.0, a <a href="https://github.com/paragonie/random_compat" class="link external">» userland implementation</a> is available for PHP 5.2 to 5.6, inclusive. </span> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.random-int-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">min</code></dt> <dd> <p class="para"> The lowest value to be returned, which must be <strong><code>PHP_INT_MIN</code></strong> or higher. </p> </dd> <dt> <code class="parameter">max</code></dt> <dd> <p class="para"> The highest value to be returned, which must be less than or equal to <strong><code>PHP_INT_MAX</code></strong>. </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-function.random-int-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a cryptographically secure random integer in the range <code class="parameter">min</code> to <code class="parameter">max</code>, inclusive. </p> </div> <div class="refsect1 errors" id="refsect1-function.random-int-errors"> <h3 class="title">Errors/Exceptions</h3> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> If an appropriate source of randomness cannot be found, an <a href="class.exception.html" class="classname">Exception</a> will be thrown. </span> </li> <li class="listitem"> <span class="simpara"> If invalid parameters are given, a <a href="class.typeerror.html" class="classname">TypeError</a> will be thrown. </span> </li> <li class="listitem"> <span class="simpara"> If <code class="parameter">max</code> is less than <code class="parameter">min</code>, an <a href="class.error.html" class="classname">Error</a> will be thrown. </span> </li> </ul> </div> <div class="refsect1 examples" id="refsect1-function.random-int-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-920"> <p><strong>Example #1 <span class="function"><strong>random_int()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">random_int</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">, </span><span style="color: #0000BB">999</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">random_int</span><span style="color: #007700">(-</span><span style="color: #0000BB">1000</span><span style="color: #007700">, </span><span style="color: #0000BB">0</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> int(248) int(-898) </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-function.random-int-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.random-bytes.html" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Generates cryptographically secure pseudo-random bytes</span></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.random-bytes.html">random_bytes</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.hash.html">Hash</a></div> <div class="up"><a href="ref.csprng.html">CSPRNG Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>