当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > mt_rand() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 mt_rand() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 537 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The mt_rand() function returns a random integer using the Mersenne Twister algorithm.
mt_rand()函数的作用是:通过使用“马其赛特旋转[Mersenne Twister]”运算法则产生一个随机整数;生成更好的随机数。

If this function is called without parameters, it returns a pseudo-random value between 0 and RAND_MAX.
如果这个函数没有设置参数,那么他将返回一个位于0到最大随机数[RAND_MAX]之间的一个伪随机值[pseudo-random value]。

If you want a random number between 10 and 100 (inclusive), use mt_rand (10,100).
如果你想获得一个位于10到100之间(包括10和100)的随机数字,你可以使用mt_rand (10,100)函数。

Syntax
语法

mt_rand(min,max)

Parameter
参数
Description
描述
min,max Optional. Specifies the range the random number should lie within
可选参数。指定返回的随机数范围;min为最小值,max为最大值


Tips and Notes
提示和注意点

Tip: This function produces a better random value four times faster than rand()!
提示:mt_rand()函数返回结果的速度比使用rand()函数快四倍!

Note: In PHP 4.2.0 and later, there is no need to seed the random generator with mt_srand(). This is done automatically.
注意:在PHP 4.2.0及其以上版本中,你不需要给mt_srand()函数指定种子,因为它是自动完成的。


Example
案例

In this example we will return some random numbers:
在这个案例中,我们将返回一些随机数字:

<?php
echo(mt_rand() . "<br />");
echo(mt_rand() . "<br />");
echo(mt_rand(10,100))
?>

The output of the code above could be:
上述代码将输出下面的结果:

1150905288
613289478
21

评论 (0) All

登陆 | 还没注册?