用户登录
用户注册

分享至

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

  • 作者: 此情可待成追忆
  • 来源: 51数据库
  • 2023-01-03

问题描述

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

How does one generate a random float between 0 and 1 in PHP?

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

I'm looking for the PHP's equivalent to Java's Math.random().

推荐答案

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

You may use the standard function: lcg_value().

这是 rand() 文档中提供的另一个函数:

Here's another function given on the rand() docs:

// auxiliary function
// returns random number with flat distribution from 0 to 1
function random_0_1() 
{
    return (float)rand() / (float)getrandmax();
}
软件
前端设计
程序设计
Java相关