PHP Rand() 函数

PHP Rand() 函数

rand() 函数用于生成随机整数。

语法:

int rand ( void )
    or
    int rand ( int min , intmax )
参数 描述 必填/可选
min 指定要返回的最低数字。 可选
max 指定要返回的最高数字。 可选

示例1

<?php
echo "Get Random number by using rand() function: ".(rand() . "<br>");
echo "Get Random number by using rand() function: ".(rand() . "<br>");
echo "<b>"."Note: Refresh page to get another randome value"."<b>";
?>

输出:

Get Random number by using rand() function: 81627923
Get Random number by using rand() function: 1857469033
Note: Refresh page to get another randome value

示例2

<?php
echo "To get random number b/w (rand(10,100)): ".(rand(10,100));
echo "<br>"."<br>"."Note: Refresh page to get new random number";
?>

输出:

To get random number b/w (rand(10,100)): 15
Note: Refresh page to get new random number

注意:mt_rand() 数学函数用于获取更好的随机值,比 rand() 函数快4倍。

示例3

<?php
r = array(0,0,0,0,0,0,0,0,0,0,0);
for (i=0;i<1000000;i++) {
  n = rand(0,100000);
  if (n<=10) {
    r[n]++;
  }
}
print_r($r); 
?>

输出:

Array ( [0] => 7 [1] => 6 [2] => 9 [3] => 15 [4] => 10 [5] => 12 [6] => 10 [7] => 14 [8] => 9 [9] => 12 [10] => 13 )

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程