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

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP 中的 srand() 函数


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

Definition and Usage
定义和用法

The srand() function seeds the random number generator for rand().
srand()函数的作用是:播下一个随机数发生器[random number generator]的种子。

This function should be called only once per script, and it must be called before any calls to rand().
这个函数最好在一个脚本中只请求一次,而且,最好是在请求rand()函数之前请求它。

Syntax
语法

srand(seed)

Parameter
参数
Description
描述
seed Optional. Seed number for the random number generator. If omitted, a random value is used.
可选参数。为随机数发生器[random number generator]播下一颗数值种子。如果忽略该参数,那么将使用随机值


Tips and Notes
提示和注意点

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 greater the randomness of the seed, the more random number you'll get. A suitable number to seed this function with, is mktime().
提示:如果你随意的播种,那么你所获得的数字的随机性将会更大。你可以使用mktime()函数为该函数指定一颗较为合适的种子。


Example
案例

In this example we will seed the random number generator:
在下面的案例中,我们将为随机数发生器[random number generator]播下一颗数值种子:

<?php
srand(mktime());
echo(rand());
?>

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

14054

评论 (0) All

登陆 | 还没注册?