What are the differences between php mt_rand () and rand ()
This article mainly introduces "what is the difference between php mt_rand () and rand ()". In daily operation, I believe many people have doubts about the difference between php mt_rand () and rand (). The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is the difference between php mt_rand () and rand ()"! Next, please follow the editor to study!
Differences: 1. If the parameter is omitted, the random number generated by rand () is between 0 and getrandmax (), while the random number generated by mt_rand () is between 0 and mt_getrandmax (); 2. The performance of mt_rand () is better than that of rand ().
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Both the rand () and mt_rand () functions produce a random integer, both of which can be used in two forms:
Int rand () int mt_rand () int rand (int $min, int $max) int mt_rand ($min, $max)
For the first form:
Random numbers generated by rand () are between 0 and getrandmax ()
Random numbers generated by mt_rand () are between 0 and mt_getrandmax ()
For the second form:
Rand () generates a random number from $min to $max
Mt_rand () generates a random number from $min to $max
Contrast:
Mt_rand () is a better random number generator because it sows a better random number seed than rand (); and its performance is 4 times faster than rand (), and the range of values represented by mt_getrandmax () is wider.
PS: generation of random floating point numbers
There is a demo in the PHP manual
Function randomFloat ($min = 0, $max = 1) {return $min + mt_rand () / mt_getrandmax () * ($max-$min);} var_dump (randomFloat ()); var_dump (randomFloat (2,20)); at this point, the study on "what's the difference between php mt_rand () and rand ()" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!