srand

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

srand播下随机数发生器种子

说明

srand(int $seed = 0, int $mode = MT_RAND_MT19937): void

使用 seed 播下随机数发生器种子,或者seed0 时,使用随时值。

注意: 不再需要用 srand()mt_srand() 给随机数发生器播种,因为现在是由系统自动完成的。

警告

由于 Mt19937(“梅森旋转算法”)引擎仅接受 32 位整数作为种子,因此尽管 Mt19937 的范围为 219937-1,但可能的随机序列数量仅限于 232(即 4,294,967,296)。

当依赖隐式或显式随机播种时,重复会出现得更早。根据生日问题,在少于 80,000 个随机生成的种子后,预计重复种子的概率为 50%。在随机生成大约 30,000 个种子后,重复种子的概率为 10%。

This makes Mt19937 unsuitable for applications where duplicated sequences must not happen with more than a negligible probability. 如果需要可重复的种子,Random\Engine\Xoshiro256StarStarRandom\Engine\PcgOneseq128XslRr64 引擎都支持更大的种子,它们不太可能随机碰撞。如果不需要再现性,Random\Engine\Secure 引擎提供加密安全随机性。

注意: 自 PHP 7.1.0 起,srand() 成为 mt_srand() 的别名。

参数

seed

任意 int 种子值。

返回值

没有返回值。

更新日志

版本 说明
7.1.0 srand() 成为 mt_srand() 的别名。

参见

  • rand() - 产生一个随机整数
  • getrandmax() - 显示随机数最大的可能值
  • mt_srand() - 播下一个更好的随机数发生器种子

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top