What are the functions in php that could generate a non-integer number from 0 to 1

    4 answers 4

    lcg_value:

    Combined linear congruential generator

    Or something like: mt_rand(0, 10)/10

    • lcg_value() didn't know great! And with mt_rand() it is desirable to maximize the upper value, for which mt_getrandmax() is suitable. With 10 will be just max. 10 different values ​​between 0 and 1 - no ice) - Sergiks
    • Well, that's an example. Instead of 10, enter the value that corresponds to the accuracy you need. - mayar
    • By the way, the LCG frequency is only 2^32 , while the Mersenne twister frequency in mt_rand() is much more: 2^19937 − 1 - Sergiks
     mt_rand() / mt_getrandmax() 

    The first function generates a random integer from 0 to the maximum integer, and the second returns this very max. whole.

    Unlike the standard rand() , mt_rand() is 4 times faster, and the results are much more "random".

       $rand = rand (0,1000); if ($rand!=0) $rand=$rand/1000; 

      This option is suitable?

        So I figured out how to generate a non-integer number from 0 to 100, for example, you need to get 36.418. The example displays 30 random numbers of this type:

          for($i = 0; $i < 30; $i++){ $rand = mt_rand(0, 100) + (mt_rand(0, 10)/10) + (mt_rand(0, 10)/100) + (mt_rand(0, 10)/1000); echo "$rand</br>"; }