PHP 8.1.15 Released!

rand

(PHP 4, PHP 5, PHP 7, PHP 8)

rand产生一个随机整数

说明

rand(): int
rand(int $min, int $max): int

如果没有提供可选参数 minmax 调用 rand() 会返回 0 到 getrandmax() 之间的伪随机整数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用 rand(5, 15)

警告

本函数并不会生成安全加密的值,并且不可用于加密或者要求返回值不可猜测的目的。

如果需要加密安全随机,则可以将 Random\Engine\Secure 引擎用于 Random\Randomizer。对于简单的用例,random_int()random_bytes() 函数提供了操作系统的 CSPRNG 支持的方便且安全的 API

注意: 在某些平台下(例如 Windows)getrandmax() 只有 32767。如果需要的范围大于 32767,那么指定 minmax 参数就可以生成更大的数了,或者考虑用 mt_rand() 来替代之。

注意: 自 PHP 7.1.0 起,rand()mt_rand() 使用相同的随机数生成器。为了保持向后兼容性,rand() 允许 max 小于 min,而不是像 mt_rand() 一样,返回 false

参数

min

返回的最低值(默认:0)

max

返回的最高值(默认:getrandmax()

返回值

介于 min (或是 0)和 max (或 getrandmax(),包含该值)之间的伪随机值。

更新日志

版本 说明
7.2.0 rand() 已收到模偏差的 错误修复。这意味着使用特定种子生成的序列可能与 64 位机器上的 PHP 7.1.0 不同。
7.1.0 rand() 成为 mt_rand() 的别名。

范例

示例 #1 rand() 例子

<?php
echo rand() . "\n";
echo
rand() . "\n";

echo
rand(5, 15);
?>

以上例程的输出类似于:

7771
22264
11

注释

警告

min max 的范围必须在 getrandmax() 范围内。即 (max - min) <= getrandmax()。否则,rand() 可能会返回质量差的随机数。

参见

  • srand() - 播下随机数发生器种子
  • getrandmax() - 显示随机数最大的可能值
  • mt_rand() - 通过梅森旋转(Mersenne Twister)随机数生成器生成随机值
  • random_int() - 生成加密安全的伪随机整数
  • random_bytes() - Get cryptographically secure random bytes

add a note

User Contributed Notes 5 notes

up
4
relsqui at armory dot com
18 years ago
Don't forget, it's faster to use bitwise operations when you need a random number that's less than some power of two. For example,

<?php
rand
()&1;
// instead of
rand(0,1);
// for generating 0 or 1,

rand()&3;
// instead of
rand(0,3);
// for generating 0, 1, 2, or 3,

rand()&7;
// instead of
rand(0,7)
// for generating 0, 1, 2, 3, 4, 5, 6, or 7,
?>

and so on. All you're doing there is generating a default random number (so PHP doesn't have to parse any arguments) and chopping off the piece that's useful to you (using a bitwise operation which is faster than even basic math).
up
-1
Anonymous
12 years ago
Generate a random 5 character A-Z0-9  string

<?php
for ($i=0; $i<6; $i++) {
   
$d=rand(1,30)%2;
    echo
$d ? chr(rand(65,90)) : chr(rand(48,57));
}
?>

# php -r 'for ($i=0; $i<6; $i++) { $d=rand(1,30)%2; echo $d ? chr(rand(65,90)) : chr(rand(48,57)); } echo "\n";'
14BW1A
up
-5
Hayley Watson
10 years ago
The Windows rand() function is quite a lot worse than merely having a low maximum value. It's an ordinary Linear Congruential Generator, which means you only need three consecutive values to be able to predict its entire future output.

Given the numbers 13050,  4267,  25352, construct the equations
4267 = (13050a+c) % 32768
25352 = (4267a+c) % 32768

Solving for a and c gives

a = 20077
c = 12345

Which means the next number that should be spat out is (25352×20077+12345) % 32768 = 19105 -- which indeed it is.

It's not the small rand_max that breaks the algorithm, it's a weakness in the  LCG algorithm itself. It's designed for when you only want a few kinda-random numbers occasionally, not if you want to generate any random-looking data.
up
-5
szeryf.wordpress.com
11 years ago
Much easier way to generate random string of numbers and letters:

<?php
$n
= rand(10e16, 10e20);
echo
base_convert($n, 10, 36);
?>

This generates strings of about 11 characters. Experiment with the range for rand() if you want shorter or longer.
up
-6
Anonymous
2 years ago
Note that the algorithm change in version 7.1.0 broke the repeatability of a random sequence initialized with a given value. For example if you have a program like:

<?php
srand
($argv[1]);
for (
$i = 0; $i < 10; $i++) {
    echo
rand().PHP_EOL;
}
?>

It will will no longer produce the same results after version 7.1.0. This can be very important for some kinds of simulations. Hopefully you were using mt_rand() or something better all along, otherwise you will have some digging to do if you want your program to be able to repeat simulations from the pre-7.1.0 days... You will need to look in the PHP source archives to discover the algorithm they used to use and replicate it in your program.
To Top