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.
?
?
?
?