<?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.mt-rand.php',
    1 => 'mt_rand',
    2 => '通过梅森旋转（Mersenne Twister）随机数生成器生成随机值',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random 函数',
  ),
  'prev' => 
  array (
    0 => 'function.mt-getrandmax.php',
    1 => 'mt_getrandmax',
  ),
  'next' => 
  array (
    0 => 'function.mt-srand.php',
    1 => 'mt_srand',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/random/functions/mt-rand.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.mt-rand" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mt_rand</h1>
   <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mt_rand</span> &mdash; <span class="dc-title">通过梅森旋转（Mersenne Twister）随机数生成器生成随机值</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.mt-rand-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mt_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>mt_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">
   很多老的 libc
   的随机数发生器具有一些不确定和未知的特性而且很慢。<span class="function"><strong>mt_rand()</strong></span>
   函数是旧的 <span class="function"><a href="function.rand.php" class="function">rand()</a></span> 的临时替代。该函数用了<a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">&raquo;&nbsp;梅森旋转</a>中已知的特性作为随机数发生器，它可以产生随机数值的平均速度比
   libc 提供的 rand() 快四倍。
  </p>
  <p class="simpara">
   如果没有提供可选参数 <code class="parameter">min</code> 和
   <code class="parameter">max</code>，<span class="function"><strong>mt_rand()</strong></span>
   返回 0 到 <span class="function"><a href="function.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>
   之间的伪随机数。例如想要 5 到 15（包括 5 和 15）之间的随机数，用
   <code class="literal">mt_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>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.mt-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.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>）
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.mt-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.mt-getrandmax.php" class="function">mt_getrandmax()</a></span>，包括两端）。
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.mt-rand-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     如果 <code class="parameter">max</code> 小于 <code class="parameter">min</code>，则会抛出
     <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> 异常。
    </span>
   </li>
  </ul>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.mt-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>8.0.0</td>
       <td>
        如果 <code class="parameter">max</code> 小于 <code class="parameter">min</code>，则会抛出
        <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> 异常。之前会抛出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 错误，并且函数返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><a href="function.rand.php" class="function">rand()</a></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"><a href="function.rand.php" class="function">rand()</a></span> <a href="migration71.incompatible.php#migration71.incompatible.rand-srand-aliases" class="link">成为</a> <span class="function"><strong>mt_rand()</strong></span> 的别名。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><strong>mt_rand()</strong></span> <a href="migration71.incompatible.php#migration71.incompatible.fixes-to-mt_rand-algorithm" class="link">成为</a>使用梅森旋转（Mersenne
        Twister）算法的固定、正确版本。要使用旧行为，请使用 <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> 并将
        <strong><code><a href="random.constants.php#constant.mt-rand-php">MT_RAND_PHP</a></code></strong> 作为第二个参数。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.mt-rand-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>mt_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">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">), </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">1604716014
1478613278
6</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.mt-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>
    范围内。即 (<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"><a href="function.rand.php" class="function">rand()</a></span> 可能会返回质量差的随机数。
   </p>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.mt-rand-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.mt-srand.php" class="function" rel="rdfs-seeAlso">mt_srand()</a> - 播下一个更好的随机数发生器种子</span></li>
    <li><span class="function"><a href="function.mt-getrandmax.php" class="function" rel="rdfs-seeAlso">mt_getrandmax()</a> - 显示随机数的最大可能值</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); ?>