uTools-Manuals/docs/php/rand.html
2019-04-28 19:00:34 +08:00

144 lines
5.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>产生一个随机整数</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.rand" class="refentry">
<div class="refnamediv">
<h1 class="refname">rand</h1>
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</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="methodparam">void</span>
) : <span class="type">int</span></div>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>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>
) : <span class="type">int</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="getrandmax.html" class="function">getrandmax()</a></span>
之间的伪随机整数。例如想要 5 到 15包括 5 和 15之间的随机数
<em>rand(5, 15)</em>
</p>
<blockquote class="note"><p><strong class="note">Note</strong>:
<span class="simpara">
在某些平台下(例如 Windows<span class="function"><a href="getrandmax.html" class="function">getrandmax()</a></span>
只有 32767。如果需要的范围大于 32767那么指定 <code class="parameter">min</code>
<code class="parameter">max</code> 参数就可以生成更大的数了,或者考虑用
<span class="function"><a href="mt_rand.html" class="function">mt_rand()</a></span> 来替代之。
</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="getrandmax.html" 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">
A pseudo random value between <code class="parameter">min</code>
(or 0) and <code class="parameter">max</code> (or <span class="function"><a href="getrandmax.html" class="function">getrandmax()</a></span>, inclusive).
</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>4.2.0</td><td>随机数发生器自动进行播种。</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-4141">
<p><strong>Example #1 <span class="function"><strong>rand()</strong></span> 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">15</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程的输出类似于:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
7771
22264
11
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.rand-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="srand.html" class="function" rel="rdfs-seeAlso">srand()</a> - 播下随机数发生器种子</span></li>
<li class="member"><span class="function"><a href="getrandmax.html" class="function" rel="rdfs-seeAlso">getrandmax()</a> - 显示随机数最大的可能值</span></li>
<li class="member"><span class="function"><a href="mt_rand.html" class="function" rel="rdfs-seeAlso">mt_rand()</a> - 生成更好的随机数</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>