Because work requires PHP to generate 0~1 random decimals, previously wrote a "PHP generation 0~1 Random Decimal method", based on Mt_rand ()And Mt_getrandmax ()Realize.
Later, a netizen commented that php native method lcg_value () can realize 0~1 random decimal generation.
Lcg_value description
float Lcg_value (void)
Lcg_value () returns a pseudo-random number with a range of (0, 1). This function combines two co-generators with a period of 2^31-85 and 2^31-249. The period of this function is equal to the product of the two primes.
Returns: the pseudo-random number of the range (0, 1).
<?phpfor ($i =0; $i <5; $i + +) { echo lcg_value (). Php_eol;}? >
Output:
0.115165158519950.0646845515752970.682751740311890.557307465290990.70215008878091
Comparison of two generation 0~1 random decimal methods
1. Execution Time Comparison
Executes 100,000 times based on the Mt_rand () and Mt_getrandmax () algorithm run time
<?php/** * Generate 0~1 random decimals * @param int $min * @param int $max * @return Float */function randfloat ($min = 0, $ Max=1) { return $min + Mt_rand ()/mt_getrandmax () * ($max-$min);} Get Microtimefunction Get_microtime () { list ($usec, $sec) = explode (' ', Microtime ()); return (float) $usec + (float) $sec;} Record start Time $starttime = Get_microtime ();//execute 100,000 times to get random decimals for ($i =0; $i <100000; $i + +) { randfloat ();} Record end Time $endtime = Get_microtime ();//Output Runtime printf ("Run time%f ms\r\n", ($endtime-$starttime) *1000);? >
Output: Run time 266.893148 Ms
Run time of Lcg_value () 100,000 times
<?php//get Microtimefunction get_microtime () { list ($usec, $sec) = Explode ("', Microtime ()); return (float) $usec + (float) $sec;} Record start Time $starttime = Get_microtime ();//execute 100,000 times to get random decimals for ($i =0; $i <100000; $i + +) { lcg_value ();} Record end Time $endtime = Get_microtime ();//Output Runtime printf ("Run time%f ms\r\n", ($endtime-$starttime) *1000);? >
Output: Run time 86.178064 Ms
Execution time comparison, because Lcg_value () is directly php native method, while Mt_rand () and Mt_getrandmax () need to call two methods, and need to calculate, so Lcg_value () execution time is about 3 times times faster.
2. Comparison of random effects
Random effects based on the Mt_rand () and Mt_getrandmax () algorithms
<?php/** * Generate 0~1 random decimals * @param int $min * @param int $max * @return Float */function randfloat ($min = 0, $ Max=1) { return $min + Mt_rand ()/mt_getrandmax () * ($max-$min);} Header (' content-type:image/png '); $im = Imagecreatetruecolor (at 255); $color 1 = imagecolorallocate ($im, 255, 255,) ; $color 2 = imagecolorallocate ($im, 0, 0, 0); for ($y =0; $y <512; $y + +) {for ($x =0; $x <512; $x + +) { $rand = randf Loat (); if (Round ($rand, 2) >=0.5) { imagesetpixel ($im, $x, $y, $color 1); } else{ imagesetpixel ($im, $x, $y, $color 2);}} Imagepng ($im); Imagedestroy ($im);? >
Random:
Random effects of Lcg_value ()
<?phpheader (' content-type:image/png '); $im = Imagecreatetruecolor (+), $color 1 = imagecolorallocate ($im, 255, 255, 255); $color 2 = imagecolorallocate ($im, 0, 0, 0); for ($y =0; $y <512; $y + +) {for ($x =0; $x <512; $x + +) { $ Rand = Lcg_value (); if (Round ($rand, 2) >=0.5) { imagesetpixel ($im, $x, $y, $color 1); } else{ imagesetpixel ($im, $x, $y, $color 2);}} Imagepng ($im); Imagedestroy ($im);? >
Random:
comparing the random effect, it can be seen that the random effect generated by using Mt_rand () and Mt_getrandmax () algorithm is more chaotic than that of Lcg_value ().
This article explains the effect of PHP lcg_value and Mt_rand generated 0~1 random decimals, more relevant content please pay attention to the PHP Chinese web.
Related recommendations;
How to format a class with ID prefixes via PHP
How to restore print_r processed data to the original array via PHP
Determine if a connection is available by using PDO in PHP