0 到 1.0 php 之间的随机浮点数
可能重复:
php中的随机浮点数
是否可以创建一个介于 0 和 1.0 之间的随机浮点数,例如 0.4、0.8 等.我使用了 rand 但它只接受整数.
Is it possible to create a random float number between 0 and 1.0 e.g 0.4, 0.8 etc. I used rand but it only accepts integers.
推荐答案
mt_rand() / mt_getrandmax();
避免使用 rand() 函数,因为它通常依赖于平台的 C rand() 实现,通常以非常简单的模式创建数字.请参阅 php.net 上的此评论
Avoid the rand() function, since it usually depends on the platform's C rand() implementation, generally creating numbers with a very simple pattern. See this comment on php.net
更新:在 php 7.1 中,rand()已更改,现在只是 mt_rand() 的别名.因此现在也可以使用 rand().
Update: In php 7.1 the rand()has been changed and is now merely an alias of mt_rand(). Therefore it is now ok to use rand(), too.
相关文章