当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > 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 中的 rand()函数


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

Definition and Usage
定义和用法

The rand() function generates a random integer.
rand()函数的作用是:产生一个随机整数。

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

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

Syntax
语法

rand(min,max)

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


Tips and Notes
注意点

Note: On some platforms (such as Windows) RAND_MAX is only 32768. So, if you require a range larger than 32768, you can specify min and max, or use the mt_rand() function instead.
注意:在某些操作平台上(例如:Windows平台),RAND_MAX的值为32768。因此,如果你需要获得一个大于32768的数值,你可以通过指定最大值和最小值来实现;或者,你也可以使用mt_rand()函数替代。

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

Tip: The mt_rand() function generates a better random value than this function!
提示:mt_rand()函数返回结果的速度比使用rand()函数快四倍!


Example
案例

In this example we will return some random numbers:
下面的案例中,我们将返回一系列随机数字:

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

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

17757
3794
97

评论 (0) All

登陆 | 还没注册?