function.mt-srand.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 139 行

HTML
139
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Seed the better random number generator</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mt-rand.html">mt_rand</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.octdec.html">octdec</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-srand" class="refentry"> <div class="refnamediv">  <h1 class="refname">mt_srand</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mt_srand</span> &mdash; <span class="dc-title">Seed the better random number generator</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>   <div class="methodsynopsis dc-description">    <span class="type"><span class="type void">void</span></span> <span class="methodname"><b><b>mt_srand</b></b></span>     ([ <span class="methodparam"><span class="type">int</span> <tt class="parameter">$seed</tt></span>   ] )</div>  <p class="para rdfs-comment">   Seeds the random number generator with   <i><tt class="parameter">seed</tt></i> or with a random value   if no <i><tt class="parameter">seed</tt></i> is given.  </p>  <blockquote><p><b class="note">Note</b>: <span class="simpara">As of PHP 4.2.0, there is no need to seed the random number generator with <a href="function.srand.html" class="function">srand()</a> or <b>mt_srand()</b> as this is now done automatically.</span></p></blockquote> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">seed</tt></i></span>     <dd>      <p class="para">       An optional seed value      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Since 4.2.0</td>       <td colspan="1" rowspan="1" align="left">        The <i><tt class="parameter">seed</tt></i> becomes optional         and defaults to a random value if omitted.       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">Since 5.2.1</td>       <td colspan="1" rowspan="1" align="left">        The Mersenne Twister implementation in PHP now uses a new seeding        algorithm by Richard Wagner. Identical seeds no longer produce the same        sequence of values they did in previous versions. This behavior is not        expected to change again, but it is considered unsafe to rely upon it        nonetheless.       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>mt_srand()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;seed&nbsp;with&nbsp;microseconds<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">make_seed</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;list(</span><span style="color: #0000BB">$usec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sec</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">'&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">());<br />&nbsp;&nbsp;return&nbsp;(float)&nbsp;</span><span style="color: #0000BB">$sec&nbsp;</span><span style="color: #007700">+&nbsp;((float)&nbsp;</span><span style="color: #0000BB">$usec&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">100000</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">mt_srand</span><span style="color: #007700">(</span><span style="color: #0000BB">make_seed</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">$randval&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.mt-rand.html" class="function" rel="rdfs-seeAlso">mt_rand()</a></li>    <li class="member"><a href="function.mt-getrandmax.html" class="function" rel="rdfs-seeAlso">mt_getrandmax()</a></li>    <li class="member"><a href="function.srand.html" class="function" rel="rdfs-seeAlso">srand()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mt-rand.html">mt_rand</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.octdec.html">octdec</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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?