Php random number rand and mt_rand

Source: Internet
Author: User
Php random numbers rand and mt_rand today I want to write a small program that throws a dice and counts the probability of points. use the following method to generate a random number for ($ j = 0; $ j & lt; 10000; $ j ++) {for ($ I = 0; $ I & lt; 6; $ I ++) {$ tempArray [php random number rand and mt_rand

Today, I want to write a program that throws a dice and counts the probability of points. use the following method to generate a random number.

For ($ j = 0; $ j <10000; $ j ++) {for ($ I = 0; $ I <6; $ I ++) {$ tempArray [$ I] = rand (1, 6);} $ temp = array_count_values ($ tempArray); for ($ I = 1; $ I <7; $ I ++) {isset ($ temp [$ I])? 0: $ temp [$ I] = 0;} print_r ($ temp );}
Finally, no matter how many cycles are performed, there will always be No 6 4, 6 5, 6 3, and 6 6 most often appear. I don't know why, finally, find the mt_rand () method in the manual and the problem will not occur.

?

Mt_rand () returns random integers using the Mersenne Twister algorithm. mt_rand () is an informal alternative to rand.

?

Description

?

If no optional parameter is providedMinAndMax, Mt_rand () returns a pseudo-random number between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (including 5 and 15), use mt_rand (5, 15 ).

Many old libc random number generators have some uncertain and unknown features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than the rand () provided by libc.

?

?

?

?

Related Article

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.