Random float number between 0 and 1.0 php(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.
这篇关于0 到 1.0 php 之间的随机浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:0 到 1.0 php 之间的随机浮点数


基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01