★ wanayoo — archive 1999 http://www.php.net/manual/function.mt-rand.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Math.
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Math.
* abs
* acos
* asin
* atan
* atan2
* base_convert
* bindec
* ceil
* cos
* decbin
* dechex
* decoct
* deg2rad
* exp
* floor
* getrandmax
* hexdec
* log
* log10
* max
* min
* mt_rand
* mt_srand
* mt_getrandmax
* number_format
* octdec
* pi
* pow
* rad2deg
* rand
* round
* sin
* sqrt
* srand
* tan
Manual: mt_rand
View the source code for this pageSearch the site



Previous page
 min
 Updated
Sat, 12 Aug 2000
mt_srand 
Next page


mt_rand

(PHP3 >= 3.0.6, PHP4 )

mt_rand -- Generate a better random value

Description

int mt_rand ([int min [, int max]])

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 rand() function. mt_rand() function is a drop-in replacement for this. It uses a random number generator with known characteristics, the Mersenne Twister, which will produce random numbers that should be suitable for cryptographic purposes and is four times faster than what the average libc provides. The Homepage of the Mersenne Twister can be found at http://www.math.keio.ac.jp/~matumoto/emt.html, and an optimized version of the MT source is available from http://www.scp.syr.edu/~marc/hawk/twister.html.

If called without the optional min, max arguments mt_rand() returns a pseudo-random value between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use mt_rand (5, 15).

Remember to seed the random number generator before use with mt_srand().

Note: In versions before 3.0.7 the meaning of max was range. To get the same results in these versions the short example should be mt_rand (5, 11) to get a random number between 5 und 15.

See also mt_srand(), mt_getrandmax(), srand(), rand() and getrandmax().


User Contributed Notes: mt_rand


mikeuser@hotmail.com
01-Dec-1999 03:36
BUG! (WinNT)


Setting min-max range doesn't work! I get values well beyond range.
<PRE>
echo "<B>mt_rand(0,20)=".mt_rand(0,20)."</B>
";


Actual results:
mt_rand(0,20)=879286
mt_rand(0,20)=1357383
mt_rand(0,20)=286308
etc...
</PRE>

A work-around using the % operator:

<PRE>
$randnum = ($mt_rand() % $maxVal-$minVal) + minVal;
</PRE>



rom@infocasatour.com
13-Apr-2000 11:30
Yes, I had the same problem with mt_rand under win2000 too... :)

The second method discribed higher working ok.





rom@infocasatour.com
13-Apr-2000 11:34
Addon...
But author of this method made two errors.
This is correct:
$rndnumber=(mt_rand() % $maxVal-$minVal) + $minVal;

Thats ok.



mrdlinux@yahoo.com
20-Jul-2000 10:02
And for those who prefer scaling:

mt_rand() / RAND_MAX * (Max - Min) + Min;



 About Notes


Previous page
 min
 Updated
Sat, 12 Aug 2000
mt_srand 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.