Why is the random number distribution in PHP extremely uneven?

Source: Internet
Author: User
When PHP generates a random number, if the range limit is too large, the generated random number is very large. Why? Test code: {code ...} result: {code ...} I have run this code for dozens of times. The result is the same. The random numbers generated are 10 ^ 17 or 10 ^ 18... when PHP generates a random number, if the range limit is too large, the generated random number is very large. Why?

Test code:


  

Result:

4821547998934728704809007189706617651215498198060923617286955922199861526528579259532523130060883225384794309263368261190596798971904359567904319576473657115882262343188483693958636641452032

I have run this code for dozens of times, and the results are the same. The random numbers generated are large numbers of the magnitude of 10 ^ 17 or 10 ^ 18 (on 64-bit systems, PHP_INT_MAX is 9223372036854775807, 9*10 ^ 18)

This applies to both rand and mt_rand. Before mt_rand () generates a random number, mt_srand () is executed. The official PHP manual says that since PHP 4.2, you do not need to manually execute t_srand.

As we all know, random numbers generated by computers are not real random numbers. PHP also officially said that they are pseudo-random numbers (pseudo-random). But why is it so powerful to PHP_INT_MAX?

Question added: If mt_rand () is directly called without parameters, the random number distribution is uneven, small (several hundred thousand) and (between 2 ^ 32 and 2 ^ 64) are not:

126193157812101522797775756971988855481179641824955196642306582590654301368501653301469521205

In addition, the random array I want is as follows:

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

Very large, very small, all have. I have been able to generate a random number with a slightly even distribution. I just want to ask why PHP's built-in random number function cannot do this.

Appendix: My improved mt_rand (0, PHP_INT_MAX)

 

--- Digress ---
@ Sha MIAO:

Therefore, using such a few broken data to say "uneven" is a great contempt for mathematics. I very much hope that the questioner can respect science and come up with arguments that can be interpreted according to statistics next time, rather than simply telling things in a superficial sense, so as to avoid common-sense jokes.

When talking about technical issues, everyone has an unfamiliar field. Even if they are skilled, they may be confused at the moment. I have been using PHP for many years and have answered many questions on this site. When I encountered this problem, it seems that it is not in line with my expectations. I tried to improve and think about it, and I couldn't figure it out. I just came to consult.

At the same time, I also made dozens of verifications myself, used Google and StackOverFlow, and found a way to get a more balanced random number of digits I wanted, it means that I have also made some efforts. Instead of posting answers to super-low-level questions, I have not come up with immature conclusions. [PHP engine is not doing well enough ].

Think of a story:

-What can I read with three dots? -I don't know. -Read lai again! -In this case, recognize the word half-what should I add to the three pieces of water? -Read qu! -Read fa, Oh, kiss-(thigh shot) Oh! Yes! Read fa! I know !!!

I think most people have had such experiences, maybe technically or in life.

Therefore, thank you for helping me answer your questions. Please do not be angry with me because I am not a high level and I am very impetuous at ordinary times, we have a rough understanding of probability on this issue. However, this posting is still very positive. It is too exaggerated to say "Break the problem" or "despise". For me, this is a good problem that has plagued me for a few days, I have no subjective willingness or skill to despise the mathematics and PHP engines.

Reply content:

When PHP generates a random number, if the range limit is too large, the generated random number is very large. Why?

Test code:


    

Result:

4821547998934728704809007189706617651215498198060923617286955922199861526528579259532523130060883225384794309263368261190596798971904359567904319576473657115882262343188483693958636641452032

I have run this code for dozens of times, and the results are the same. The random numbers generated are large numbers of the magnitude of 10 ^ 17 or 10 ^ 18 (on 64-bit systems, PHP_INT_MAX is 9223372036854775807, 9*10 ^ 18)

This applies to both rand and mt_rand. Before mt_rand () generates a random number, mt_srand () is executed. The official PHP manual says that since PHP 4.2, you do not need to manually execute t_srand.

As we all know, random numbers generated by computers are not real random numbers. PHP also officially said that they are pseudo-random numbers (pseudo-random). But why is it so powerful to PHP_INT_MAX?

Question added: If mt_rand () is directly called without parameters, the random number distribution is uneven, small (several hundred thousand) and (between 2 ^ 32 and 2 ^ 64) are not:

126193157812101522797775756971988855481179641824955196642306582590654301368501653301469521205

In addition, the random array I want is as follows:

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

Very large, very small, all have. I have been able to generate a random number with a slightly even distribution. I just want to ask why PHP's built-in random number function cannot do this.

Appendix: My improved mt_rand (0, PHP_INT_MAX)

 

--- Digress ---
@ Sha MIAO:

Therefore, using such a few broken data to say "uneven" is a great contempt for mathematics. I very much hope that the questioner can respect science and come up with arguments that can be interpreted according to statistics next time, rather than simply telling things in a superficial sense, so as to avoid common-sense jokes.

When talking about technical issues, everyone has an unfamiliar field. Even if they are skilled, they may be confused at the moment. I have been using PHP for many years and have answered many questions on this site. When I encountered this problem, it seems that it is not in line with my expectations. I tried to improve and think about it, and I couldn't figure it out. I just came to consult.

At the same time, I also made dozens of verifications myself, used Google and StackOverFlow, and found a way to get a more balanced random number of digits I wanted, it means that I have also made some efforts. Instead of posting answers to super-low-level questions, I have not come up with immature conclusions. [PHP engine is not doing well enough ].

Think of a story:

-What can I read with three dots? -I don't know. -Read lai again! -In this case, recognize the word half-what should I add to the three pieces of water? -Read qu! -Read fa, Oh, kiss-(thigh shot) Oh! Yes! Read fa! I know !!!

I think most people have had such experiences, maybe technically or in life.

Therefore, thank you for helping me answer your questions. Please do not be angry with me because I am not a high level and I am very impetuous at ordinary times, we have a rough understanding of probability on this issue. However, this posting is still very positive. It is too exaggerated to say "Break the problem" or "despise". For me, this is a good problem that has plagued me for a few days, I have no subjective willingness or skill to despise the mathematics and PHP engines.

Your range is a 19-digit integer (probably, don't go into it ). What you see is "very big", but it is "very long" and it is only 19 people.

But among all the non-negative integers of <10 ^ 19, the total number of 19 digits is(10^19-10^18) / 10^19 = 90%.

That is to say, if you want to follow the "different lengths" Distribution you expect, you can only achieve this if the probability is significantly skewed to the 10% side. This is ridiculous.

Statistics: statistics are called statistics only when the quantity is very large. To test the uniformity of the random function distribution, we should use the data size above 10 ^ 7 in experience to obtain that the error is acceptable within 5%.

Therefore, using such a few broken data to say "uneven" is a great contempt for mathematics. I very much hope that the questioner can respect science and come up with arguments that can be interpreted according to statistics next time, rather than simply telling things in a superficial sense, so as to avoid 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. the random number is finally calculated by the algorithm, and the random number tends infinitely to be a 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.