This article mainly introduces the PHP implementation to generate 0~1 random decimal method, interested in the friend's reference, I hope to help you.
JavaScript generates 0~1 random decimals in a way that calls its own math.random ();
For example:
<script type= "Text/javascript" >document.write (Math.random ()); 0.5840498607140034</script>
There are Rand,mt_rand random methods in PHP, but neither of these methods generates 0~1 random decimals, and we can write a method to implement this function.
PHP generates 0~1 random decimal methods as follows:
<?php/** * Generate 0~1 random decimals * @param int $min * @param int $max * @return Float */function randfloat ($min =0, $max =1) { C2/>return $min + mt_rand ()/mt_getrandmax () * ($max-$min);} example, create 5 0~1 random decimals for ($i =0; $i <5; $i + +) { echo randfloat (). ' <br> ';}? >
Output:
0.598040262515680.677721965442280.905897512056820.450878588227030.17475316774787
The above is the whole content of this article, I hope that everyone's study has helped.