<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >rand</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Math 数学函数" HREF="ref.math.html"><LINK REL="PREVIOUS" TITLE="rad2deg" HREF="function.rad2deg.html"><LINK REL="NEXT" TITLE="round" HREF="function.round.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.rad2deg.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.round.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.rand" ></A >rand</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN109011" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >rand -- 产生一个随机整数</DIV ><DIV CLASS="refsect1" ><A NAME="AEN109014" ></A ><H2 >说明</H2 >int <B CLASS="methodname" >rand</B > ( [int min, int max] )<BR ></BR ><P > 如果没有提供可选参数 <CODE CLASS="parameter" >min</CODE > 和 <CODE CLASS="parameter" >max</CODE >,<B CLASS="function" >rand()</B > 返回 0 到 <TT CLASS="constant" ><B >RAND_MAX</B ></TT > 之间的伪随机整数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用 <TT CLASS="literal" >rand(5, 15)</TT >。 </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN109032" ></A ><P ><B >例 1. <B CLASS="function" >rand()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">echo </font><font color="#0000BB">rand</font><font color="#007700">() . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">rand</font><font color="#007700">() . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br />echo </font><font color="#0000BB">rand</font><font color="#007700">(</font><font color="#0000BB">5</font><font color="#007700">, </font><font color="#0000BB">15</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例的输出类似于:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >7771 22264 11</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 在某些平台下(例如 Windows)<TT CLASS="constant" ><B >RAND_MAX</B ></TT > 只有 32768。如果需要的范围大于 32768,那么指定 <CODE CLASS="parameter" >min</CODE > 和 <CODE CLASS="parameter" >max</CODE > 参数就可以生成大于 RAND_MAX的数了,或者考虑用 <A HREF="function.mt-rand.html" ><B CLASS="function" >mt_rand()</B ></A > 来替代之。 </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >自 PHP 4.2.0 起,不再需要用 <A HREF="function.srand.html" ><B CLASS="function" >srand()</B ></A > 或 <A HREF="function.mt-srand.html" ><B CLASS="function" >mt_srand()</B ></A > 函数给随机数发生器播种,现已自动完成。</P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 在 3.0.7 之前的版本中,<CODE CLASS="parameter" >max</CODE > 的含义是 <CODE CLASS="parameter" >range</CODE >。要在这些版本中得到和上例相同 5 到 15 的随机数,简短的例子是 <TT CLASS="literal" >rand (5, 11)</TT >。 </P ></BLOCKQUOTE ></DIV ><P > 参见 <A HREF="function.srand.html" ><B CLASS="function" >srand()</B ></A >,<A HREF="function.getrandmax.html" ><B CLASS="function" >getrandmax()</B ></A > 和 <A HREF="function.mt-rand.html" ><B CLASS="function" >mt_rand()</B ></A >。 </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.rad2deg.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.round.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >rad2deg</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.math.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >round</TD ></TR ></TABLE ></DIV ></BODY ></HTML >