<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.rand.php',
    1 => 'rand',
    2 => '产生一个随机整数',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random 函数',
  ),
  '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' => 'zh',
    '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">产生一个随机整数</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.rand-description">
  <h3 class="title">说明</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">
   如果没有提供可选参数 <code class="parameter">min</code> 和
   <code class="parameter">max</code> 调用 <span class="function"><strong>rand()</strong></span>
   会返回 0 到 <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>
   之间的伪随机整数。例如想要 5 到 15（包括 5 和 15）之间的随机数，用
   <code class="literal">rand(5, 15)</code>。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="para">
  本函数并不会生成安全加密的值，并且<em>不可</em>用于加密或者要求返回值不可猜测的目的。
 </p>
 <p class="para">
  如果需要加密安全随机，则可以将 <span class="classname"><a href="class.random-engine-secure.php" class="classname">Random\Engine\Secure</a></span> 引擎用于
  <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span>。对于简单的用例，<span class="function"><a href="function.random-int.php" class="function">random_int()</a></span>
  和 <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span> 函数提供了操作系统的 <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> 支持的方便且安全的 <abbr title="Application Programming Interface">API</abbr>。
 </p>
</div>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="simpara">
    此函数使用全局 Mt19937（“梅森旋转算法”）实例作为随机源，因此与所有其他使用全局 Mt19937 的函数共享其状态。
    使用这些函数中的任何一个都会推进<em>所有</em>其他函数的序列，无论作用域如何。
  </p>
  <p class="simpara">
    通过向 <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> 或 <span class="function"><a href="function.srand.php" class="function">srand()</a></span> 以已知值播种来生成可重复的序列也将从此函数产生可重复的输出。
  </p>
  <p class="simpara">
    在所有新编写的代码中，建议使用 <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> 的方法。
 </p>
</div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    在 PHP 7.1.0 之前，<span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>
    在某些平台（如 Windows）上仅为 32767。如果需要的范围大于 32767，那么指定 <code class="parameter">min</code>
    和 <code class="parameter">max</code> 参数就可以生成更大的数了，或者考虑用
    <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> 来替代之。
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    自 PHP 7.1.0 起，<span class="function"><strong>rand()</strong></span> 和 <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span>
    使用相同的随机数生成器。为了保持向后兼容性，<span class="function"><strong>rand()</strong></span> 允许
    <code class="parameter">max</code> 小于 <code class="parameter">min</code>，而不是像
    <span class="function"><a href="function.mt-rand.php" class="function">mt_rand()</a></span> 一样，返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
   </span>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.rand-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">min</code></dt>
     <dd>
      <p class="para">
       返回的最低值（默认：0）
      </p>
     </dd>
    
    
     <dt><code class="parameter">max</code></dt>
     <dd>
      <p class="para">
       返回的最高值（默认：<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">返回值</h3>
  <p class="para">
   介于 <code class="parameter">min</code> （或是 0）和 <code class="parameter">max</code> （或
   <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>，包含该值）之间的伪随机值。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.rand-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</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">错误修复</a>。这意味着使用特定种子生成的序列可能与
        64 位机器上的 PHP 7.1.0 不同。
       </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">成为</a> <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">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>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">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>以上示例的输出类似于：</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">注释</h3>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    <code class="parameter">min</code> <code class="parameter">max</code> 的范围必须在 <span class="function"><a href="function.getrandmax.php" class="function">getrandmax()</a></span>
    范围内。即 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>。否则，<span class="function"><strong>rand()</strong></span> 可能会返回质量差的随机数。
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.rand-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.srand.php" class="function" rel="rdfs-seeAlso">srand()</a> - 播下随机数发生器种子</span></li>
    <li><span class="function"><a href="function.getrandmax.php" class="function" rel="rdfs-seeAlso">getrandmax()</a> - 显示随机数最大的可能值</span></li>
    <li><span class="function"><a href="function.mt-rand.php" class="function" rel="rdfs-seeAlso">mt_rand()</a> - 通过梅森旋转（Mersenne Twister）随机数生成器生成随机值</span></li>
    <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - 获取生成加密安全、均匀分布的整数</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); ?>