PHP 中 0 到 1 之间的随机浮点数

2022-08-30 11:29:49

如何在PHP中生成0和1之间的随机浮点数?

我正在寻找与Java等效的PHP。Math.random()


答案 1

您可以使用标准函数:lcg_value()

下面是在 rand() docs 上给出的另一个函数:

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

答案 2

来自文档的示例:

function random_float ($min,$max) {
   return ($min+lcg_value()*(abs($max-$min)));
}

推荐