PHP 8.3.0 RC 6 available for testing

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), "\n";
?>

以上示例的输出类似于:

 
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 4 notes

up
2
play dot it at play-it dot net
7 months ago
Here is a simple base64 random string function

<?php
function random_string($length) {
$str = random_bytes($length);
$str = base64_encode($str);
$str = str_replace(["+", "/", "="], "", $str);
$str = substr($str, 0, $length);
return
$str;
}

/*
Example outputs for random_string(32)

OP0vOJEsSvr6wbgN4jIwqMMstlpMSUsl
2IHaIxD2W4VTKZuzioudbpQCALdl6Ym6
QY0eZ3QYy3OKKN6ttzbDwAwsAfkXfQ2f
jznjlPCUDbYzOTJysPP414BbdVNu4jmT
GlktgJ8JUhdH5MfQ1PHl0wnqXQlKggQs
Pb9WALM3KcGCCPBKXsPgNfy3M0Xj4aEu
AED6OTVl8aBbspdxoXvA1sT4ein8lruH
9cSbz4FhoI4qSsPZFMh0u1rWDDEgQxI2
iSBlT4K7Ad516qPXgPReSj2tii7TAK5b
DuX8HByMb2e8IdM4j49Td2JTI9Ki7o1C
*/
?>
up
2
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
-3
Anonymous
3 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.
up
-6
Anonymous
13 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
To Top