📄 function.rand.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Generate a random integer</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.rad2deg.html">rad2deg</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.round.html">round</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.rand" class="refentry"> <div class="refnamediv"> <h1 class="refname">rand</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">rand</span> — <span class="dc-title">Generate a random integer</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>rand</b></b></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>rand</b></b></span> ( <span class="methodparam"><span class="type">int</span> <tt class="parameter">$min</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$max</tt></span> )</div> <p class="simpara"> If called without the optional <i><tt class="parameter">min</tt></i>, <i><tt class="parameter">max</tt></i> arguments <b>rand()</b> returns a pseudo-random integer between 0 and <a href="function.getrandmax.html" class="function">getrandmax()</a>. If you want a random number between 5 and 15 (inclusive), for example, use <i>rand(5, 15)</i>. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> On some platforms (such as Windows), <a href="function.getrandmax.html" class="function">getrandmax()</a> is only 32768. If you require a range larger than 32768, specifying <i><tt class="parameter">min</tt></i> and <i><tt class="parameter">max</tt></i> will allow you to create a range larger than this, or consider using <a href="function.mt-rand.html" class="function">mt_rand()</a> instead. </span> </p></blockquote> <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 <a href="function.mt-srand.html" class="function">mt_srand()</a> 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">min</tt></i></span> <dd> <p class="para"> The lowest value to return (default: 0) </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">max</tt></i></span> <dd> <p class="para"> The highest value to return (default: <a href="function.getrandmax.html" class="function">getrandmax()</a>) </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A pseudo random value between <i><tt class="parameter">min</tt></i> (or 0) and <i><tt class="parameter">max</tt></i> (or <a href="function.getrandmax.html" class="function">getrandmax()</a>, inclusive). </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 3.0.7</td> <td colspan="1" rowspan="1" align="left"> In versions before 3.0.7 the meaning of <i><tt class="parameter">max</tt></i> was <i><tt class="parameter">range</tt></i>. To get the same results in these versions the short example should be <i>rand (5, 11)</i> to get a random number between 5 and 15. </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>rand()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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">);<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"><pre><div class="cdata"><pre>77712226411</pre></div> </pre></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.srand.html" class="function" rel="rdfs-seeAlso">srand()</a></li> <li class="member"><a href="function.getrandmax.html" class="function" rel="rdfs-seeAlso">getrandmax()</a></li> <li class="member"><a href="function.mt-rand.html" class="function" rel="rdfs-seeAlso">mt_rand()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.rad2deg.html">rad2deg</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.round.html">round</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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -