How to generate 0~1 random decimal method via PHP

Source: Internet
Author: User
Tags mysql functions
JavaScript generates 0~1 random decimals in a way that can call 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) {    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

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>

This article explains the generation of 0~1 random decimal method through PHP, more relevant content focus on PHP Chinese web.

Related recommendations:

Instructions for using MySQL timestamp format function from_unixtime

Instructions for using MySQL functions concat and Group_concat

Explanation of how MySQL InnoDB startup failure cannot be restarted

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.