Random Float between 0 and 1 in PHP

前端 未结 13 2719
粉色の甜心
粉色の甜心 2020-12-01 23:44

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().

13条回答
  •  半阙折子戏
    2020-12-02 00:11

    class SomeHelper
    {
         /**
         * Generate random float number.
         *
         * @param float|int $min
         * @param float|int $max
         * @return float
         */
        public static function rand($min = 0, $max = 1)
        {
            return ($min + ($max - $min) * (mt_rand() / mt_getrandmax()));
        }
    }
    

提交回复
热议问题