Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between php mt_rand () and rand ()

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report