Search code examples
phprandom

Random Float between 0 and 1 in PHP


How does one generate a random float between 0 and 1 in PHP?

I'm looking for the PHP's equivalent to Java's Math.random().


Solution

  • You may use the standard function: lcg_value().

    Here's another function given on the rand() docs:

    // auxiliary function
    // returns random number with flat distribution from 0 to 1
    function random_0_1() 
    {
        return (float)rand() / (float)getrandmax();
    }