Why is the random number distribution of PHP generated very uneven?

Source: Internet
Author: User
When PHP generates random numbers, if the interval upper limit is too large, the generated random number is a very large number, which is why?

Test code:


  
   

结果:

4821547998934728704809007189706617651215498198060923617286955922199861526528579259532523130060883225384794309263368261190596798971904359567904319576473657115882262343188483693958636641452032

我运行了几十次这段代码,结果都是如此,产生的随机数都是10^17或者10^18这个量级的大数(64位系统上,PHP_INT_MAX是9223372036854775807,即9*10^18)

rand, mt_rand都是这样,每次mt_rand()生成随机数之前,执行mt_srand()也还是这样,PHP官方手册说了,自PHP 4.2起,已经不需要手工执行t_srand()了。

众所周知,计算机生成的随机数都不是真正的随机数,PHP官方也说了是伪随机数(pseudo-random ),但是,为什么会向PHP_INT_MAX倾斜得这么厉害?

问题补充:如果不加参数直接调mt_rand(),得到的随机数分布也很不均匀,小的(几百几千)和大(介于2^32和2^64之间)的都没有:

126193157812101522797775756971988855481179641824955196642306582590654301368501653301469521205

再补充,我希望得到的随机数组是这样的:

Array( [31] => 301487779840 [57] => 10457165676412928 [32] => 2516742 [16] => 786840 [39] => 2142457578485972992 [81] => 16 [27] => 5472146880987136 [24] => 4979452 [6] => 24912932785618944 [48] => 930 [82] => 9223372036854775807)

很大的,很小的,都有。我已经有办法生成分布略均匀的随机数了,只是想请教一下大家,为什么PHP内置的随机数函数做不到这样。

附:我改良的mt_rand(0, PHP_INT_MAX)

 

--- 题外话 ---
@沙渺 在解答问题的同时说:

所以用这么几个破数据就说“不均匀”,这是对数学的极大蔑视。非常希望提问者下次能够尊重科学,拿出可以按统计学解读的论据,而不是只凭一些粗浅的直观感觉说事,以免再出现常识性的笑话。

讨论技术问题嘛,人人都有认识不深的领域,即使再熟练,也会有一时想不清楚的时候。我用PHP多年,也在本站解答了很多问题,碰到这个问题,表面看起来与我的预期不符,努力改进和思考了,也想不通,我就来请教了。

同时我自己也做了几十次的验证,使用了Google和StackOverFlow,也想了办法得到我想要的数字位数更加均衡的随机数,说明我也是做了一些努力的,不是一碰到超级低级的问题就上来发贴坐等答案,更没有拿着不成熟的结论上来喷【PHP引擎做得不够好】。

想起个小故事:

- 三点水加个来读什么?- 不知道。- 还读lai呀!- 这样啊,认字认半边- 那三点水加个去读什么呢?- 读qu!- 读fa哦,亲- (拍大腿)哎呀!是的!读fa!我认识的!!!

我想多数人都有过这样的经历吧,也许是在技术上,也许是在生活上。

所以,谢谢大家帮我解答问题,也请大家不要因为我这个问题太生气,我这个人水平确实不高,平时也很浮躁,在这个问题上对概率的认识是很粗浅。但这次发贴子,态度还是比较端正的,说“破问题”、“蔑视”就太夸张啦,对我而言,这是一个好问题,困扰我几天的问题,我也没有蔑视数学和PHP引擎的主观意愿和本事。

Reply content:

When PHP generates random numbers, if the interval upper limit is too large, the generated random number is a very large number, which is why?

Test code:


  
     

结果:

4821547998934728704809007189706617651215498198060923617286955922199861526528579259532523130060883225384794309263368261190596798971904359567904319576473657115882262343188483693958636641452032

我运行了几十次这段代码,结果都是如此,产生的随机数都是10^17或者10^18这个量级的大数(64位系统上,PHP_INT_MAX是9223372036854775807,即9*10^18)

rand, mt_rand都是这样,每次mt_rand()生成随机数之前,执行mt_srand()也还是这样,PHP官方手册说了,自PHP 4.2起,已经不需要手工执行t_srand()了。

众所周知,计算机生成的随机数都不是真正的随机数,PHP官方也说了是伪随机数(pseudo-random ),但是,为什么会向PHP_INT_MAX倾斜得这么厉害?

问题补充:如果不加参数直接调mt_rand(),得到的随机数分布也很不均匀,小的(几百几千)和大(介于2^32和2^64之间)的都没有:

126193157812101522797775756971988855481179641824955196642306582590654301368501653301469521205

再补充,我希望得到的随机数组是这样的:

Array( [31] => 301487779840 [57] => 10457165676412928 [32] => 2516742 [16] => 786840 [39] => 2142457578485972992 [81] => 16 [27] => 5472146880987136 [24] => 4979452 [6] => 24912932785618944 [48] => 930 [82] => 9223372036854775807)

很大的,很小的,都有。我已经有办法生成分布略均匀的随机数了,只是想请教一下大家,为什么PHP内置的随机数函数做不到这样。

附:我改良的mt_rand(0, PHP_INT_MAX)

 

--- 题外话 ---
@沙渺 在解答问题的同时说:

所以用这么几个破数据就说“不均匀”,这是对数学的极大蔑视。非常希望提问者下次能够尊重科学,拿出可以按统计学解读的论据,而不是只凭一些粗浅的直观感觉说事,以免再出现常识性的笑话。

讨论技术问题嘛,人人都有认识不深的领域,即使再熟练,也会有一时想不清楚的时候。我用PHP多年,也在本站解答了很多问题,碰到这个问题,表面看起来与我的预期不符,努力改进和思考了,也想不通,我就来请教了。

同时我自己也做了几十次的验证,使用了Google和StackOverFlow,也想了办法得到我想要的数字位数更加均衡的随机数,说明我也是做了一些努力的,不是一碰到超级低级的问题就上来发贴坐等答案,更没有拿着不成熟的结论上来喷【PHP引擎做得不够好】。

想起个小故事:

- 三点水加个来读什么?- 不知道。- 还读lai呀!- 这样啊,认字认半边- 那三点水加个去读什么呢?- 读qu!- 读fa哦,亲- (拍大腿)哎呀!是的!读fa!我认识的!!!

我想多数人都有过这样的经历吧,也许是在技术上,也许是在生活上。

所以,谢谢大家帮我解答问题,也请大家不要因为我这个问题太生气,我这个人水平确实不高,平时也很浮躁,在这个问题上对概率的认识是很粗浅。但这次发贴子,态度还是比较端正的,说“破问题”、“蔑视”就太夸张啦,对我而言,这是一个好问题,困扰我几天的问题,我也没有蔑视数学和PHP引擎的主观意愿和本事。

Your range is a 19-bit integer (presumably, don't want to delve into it). And you see the "very big" is actually "very long", the full 19 bit just.

But in all <10^19 non-negative integers, the number of full 19 bits is more than the total number of (10^19-10^18)/10^19 = 90% .

That is, if you want to follow the kind of "long and short" distribution that you expect, only the probability will be tilted to the side of 10%. This is absurd.

Statistics, statistics, only a very large number is called statistics. To actually test the uniformity of the distribution of random functions, experience is to use more than 10^7 data volume to run, the error can be accepted within 5%.

So with such a few data to say "uneven", this is a great contempt for mathematics. I very much hope that the next time the questioner can respect the science, come up with a statistical interpretation of the argument, rather than just some superficial intuitive feeling to say things, so as not to appear common sense jokes.

First, your sample is indeed too small. Second, the random number in the program does seem to be a pseudo-random number, random to the last or the algorithm, the infinite trend to random number pseudo-random number.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.