PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<mt_getrandmaxmt_srand>
Last updated: Mon, 16 Jul 2012

mt_rand

(PHP 3 >= 3.0.6, PHP 4, PHP 5)

mt_rand -- 生成更好的随机数

说明

int mt_rand ( [int min, int max] )

很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。mt_rand() 函数是非正式用来替换它的。该函数用了 Mersenne Twister 中已知的特性作为随机数发生器,它可以产生可作为某些加密算法种子的随机数(细节见主页)而且比通常 libc 提供的速度快四倍。

如果没有提供可选参数 minmaxmt_rand() 返回 0 到 RAND_MAX 之间的伪随机数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用 mt_rand(5, 15)

注: 自 PHP 4.2.0 起,不再需要用 srand()mt_srand() 函数给随机数生成器播种,现已自动完成。

注: 在 3.0.7 之前的版本中,max 的含义是 range。要在这些版本中得到和上例相同 5 到 15 的随机数,简短的例子是 mt_rand (5, 11)

参见 mt_srand()mt_getrandmax()rand()




<mt_getrandmaxmt_srand>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST