Random Float between 0 and 1 in PHP

前端 未结 13 2731
粉色の甜心
粉色の甜心 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-01 23:46

    Solution for PHP 7. Generates random number in [0,1). i.e. includes 0 and excludes 1.

    function random_float() {
        return random_int(0, PHP_INT_MAX-1)/PHP_INT_MAX;
    }
    

提交回复
热议问题