百科狗-知识改变命运!
--

mt_srand() - 播下一个更好的随机数发生器种子 - php 数学函数

乐乐12个月前 (11-21)阅读数 18#技术干货
文章标签随机数

mt_srand()

(PHP 4, PHP 5, PHP 7)

播下一个更好的随机数发生器种子

说明

mt_srand([int $seed]): void

mt_srand() - 播下一个更好的随机数发生器种子 - php 数学函数

用$seed来给随机数发生器播种。没有设定$seed参数时,会被设为随时数。

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

参数

$seed

可选的种子值

返回值

没有返回值。

更新日志

版本说明
4.2.0The$seedbecomes optional and defaults to a random value if omitted.
5.2.1The 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.

范例

Example #1mt_srand()例子

参见

  • mt_rand() 生成更好的随机数
  • mt_getrandmax() 显示随机数的最大可能值
  • srand() 播下随机数发生器种子
I think Joe was a little confused by the wording. The note meant that implementations of mt_rand() before the change would generate a different set of pseudorandom numbers than would implementations of mt_rand() after the change for the same seed.
That's how it reads for me, anyway.
Looks like mt_rand() gives same result for different seeds when the lowest bits are different only. Try this:
#!/usr/bin/php -q

This is a snapshop of the results:
...
mt_srand( 0xfffffffc ): mt_rand() == 0x0a223d97 == 170016151
mt_srand( 0xfffffffd ): mt_rand() == 0x0a223d97 == 170016151
mt_srand( 0xfffffffe ): mt_rand() == 0x350a9509 == 889885961
mt_srand( 0xffffffff ): mt_rand() == 0x350a9509 == 889885961
mt_srand( 0x00000000 ): mt_rand() == 0x71228443 == 1898087491
mt_srand( 0x00000001 ): mt_rand() == 0x71228443 == 1898087491
mt_srand( 0x00000002 ): mt_rand() == 0x4e0a2cdd == 1309289693
mt_srand( 0x00000003 ): mt_rand() == 0x4e0a2cdd == 1309289693
...
I found this occationally. I have no idea if it is a bug or not. In my real life I do not intend to use sequentional seeds. However, probably this may be important for somebody.
used the little script from mrcheezy at hotmail dot com and got much better results using
mt_srand(crc32(microtime()));
@ fasaxc at yahoo dot com:
If you want truly random numbers, use a truly random source. Your system is rather unwieldy when you can simply call openssl_random_pseudo_bytes() for good randomness. Don't use microtime as a source of randomness.
If you are new with seeding read my note.
I now understood seeding as a start-state of an algorithm. This algorithm generates a series of -following- pseudorandom numbers.
If you start generating from the same startvalue twice, you get the same series of random numbers twice in a row.
mt_srand(10); //start of your algorithm equals seeding set to 10
for($i=0;$i

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)