mt_srand

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

mt_srand播下一個(gè)更好的隨機(jī)數(shù)發(fā)生器種子

說明

mt_srand(int $seed = ?): void

seed 來給隨機(jī)數(shù)發(fā)生器播種。 沒有設(shè)定 seed 參數(shù)時(shí),會(huì)被設(shè)為隨時(shí)數(shù)。

注意: 不再需要用 srand()mt_srand() 給隨機(jī)數(shù)發(fā)生器播種,因?yàn)楝F(xiàn)在是由系統(tǒng)自動(dòng)完成的。

參數(shù)

seed

可選的種子值

返回值

沒有返回值。

更新日志

版本 說明
4.2.0 The seed becomes optional and defaults to a random value if omitted.
5.2.1 The Mersenne Twister implementation in PHP now uses a new seeding algorithm by Richard Wagner. Identical seeds no longer produce the same sequence of values they did in previous versions. This behavior is not expected to change again, but it is considered unsafe to rely upon it nonetheless.

范例

示例 #1 mt_srand() 例子

<?php
// seed with microseconds
function make_seed()
{
  list(
$usec$sec) = explode(' 'microtime());
  return (float) 
$sec + ((float) $usec 100000);
}
mt_srand(make_seed());
$randval mt_rand();
?>

參見

  • mt_rand() - 生成更好的隨機(jī)數(shù)
  • mt_getrandmax() - 顯示隨機(jī)數(shù)的最大可能值
  • srand() - 播下隨機(jī)數(shù)發(fā)生器種子