<HTML ><HEAD ><TITLE >mt_rand</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Mathematical Functions" HREF="ref.math.html"><LINK REL="PREVIOUS" TITLE="mt_getrandmax" HREF="function.mt-getrandmax.html"><LINK REL="NEXT" TITLE="mt_srand" HREF="function.mt-srand.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.mt-getrandmax.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.mt-srand.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.mt-rand" ></A >mt_rand</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN45023" ></A ><P > (PHP 3>= 3.0.6, PHP 4 )</P >mt_rand -- Generate a better random value</DIV ><DIV CLASS="refsect1" ><A NAME="AEN45026" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >mt_rand</B > ( [int min, int max])<BR ></BR ><P > Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the <A HREF="function.rand.html" ><B CLASS="function" >rand()</B ></A > function. The <B CLASS="function" >mt_rand()</B > function is a drop-in replacement for this. It uses a random number generator with known characteristics using the <A HREF="http://www.math.keio.ac.jp/~matumoto/emt.html" TARGET="_top" > Mersenne Twister</A >, which will produce random numbers that should be suitable for seeding some kinds of cryptography (see the home page for details) and is four times faster than what the average libc provides. </P ><P > If called without the optional <TT CLASS="parameter" ><I >min</I ></TT >, <TT CLASS="parameter" ><I >max</I ></TT > arguments <B CLASS="function" >mt_rand()</B > returns a pseudo-random value between 0 and <TT CLASS="constant" ><B >RAND_MAX</B ></TT >. If you want a random number between 5 and 15 (inclusive), for example, use <TT CLASS="literal" >mt_rand (5, 15)</TT >. </P ><P > In older versions of PHP, you had to seed the random number generator before use with <A HREF="function.mt-srand.html" ><B CLASS="function" >mt_srand()</B ></A >. Since 4.2.0 this is no longer necessary. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > In versions before 3.0.7 the meaning of <TT CLASS="parameter" ><I >max</I ></TT > was <TT CLASS="parameter" ><I >range</I ></TT >. To get the same results in these versions the short example should be <TT CLASS="literal" >mt_rand (5, 11)</TT > to get a random number between 5 and 15. </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.mt-srand.html" ><B CLASS="function" >mt_srand()</B ></A >, <A HREF="function.mt-getrandmax.html" ><B CLASS="function" >mt_getrandmax()</B ></A > and <A HREF="function.rand.html" ><B CLASS="function" >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.mt-getrandmax.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.mt-srand.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >mt_getrandmax</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.math.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >mt_srand</TD ></TR ></TABLE ></DIV ></BODY ></HTML >