PHP implementation method of generating 0~1 random decimals

Source: Internet
Author: User
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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.