php 0到1.0之间的随机浮点数php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14155603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:45:56  来源:igfitidea点击:

Random float number between 0 and 1.0 php

phprandomfloating-pointinteger

提问by Matthew Underwood

Possible Duplicate:
Random Float in php

可能的重复:
php 中的随机浮点数

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.

是否可以创建一个介于 0 和 1.0 之间的随机浮点数,例如 0.4、0.8 等。我使用了 rand 但它只接受整数。

回答by Francois Bourgeois

mt_rand() / mt_getrandmax();

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

避免使用该rand()函数,因为它通常取决于平台的 Crand()实现,通常使用非常简单的模式创建数字。在 php.net 上查看此评论

回答by h2ooooooo

What about simply dividing by 10?

简单地除以 10 怎么样?

$randomFloat = rand(0, 10) / 10;
var_dump($randomFloat);

//eg. float(0.7)

回答by Paolo

$v = mt_rand() / mt_getrandmax();

will do that.

会这样做。



In case you want only one decimal place (as in the examples from the question) just round()the value you get...

如果您只想要一个小数位(如问题中的示例),round()那么您只需要获得的值...

$v = round( $v, 1 );

...or calculate a number between 0 and 10 and divide by 10:

...或计算 0 到 10 之间的数字并除以 10:

$v = mt_rand( 0, 10 ) / 10;

回答by Nemo Caligo

According to the PHP documentation, if you're using PHP 7 you can generate a cryptographically secure pseudorandom integer using random_int

根据 PHP 文档,如果您使用的是 PHP 7,则可以使用random_int生成加密安全的伪随机整数

With that said, here's a function that utilizes this to generate a random float between two numbers:

话虽如此,这是一个利用它在两个数字之间生成随机浮点数的函数:

function random_float($min, $max) {
    return random_int($min, $max - 1) + (random_int(0, PHP_INT_MAX - 1) / PHP_INT_MAX );
}

Although random_int() is more secure than mt_rand(), keep in mind that it's also slower.

尽管 random_int() 比 mt_rand() 更安全,但请记住,它也更慢。

A previous version of this answer suggested you use PHP rand(), and had a horrible implementation. I wanted to change my answer without repeating what others had already stated, and now here we are.

此答案的先前版本建议您使用 PHP rand(),并且实现方式很糟糕。我想在不重复其他人已经说过的内容的情况下更改我的答案,现在我们来了。

回答by Dracony

how about this simple solution:

这个简单的解决方案怎么样:

abs(1-mt_rand()/mt_rand()) 

or

或者

/**
 * Generate Float Random Number
 *
 * @param float $Min Minimal value
 * @param float $Max Maximal value
 * @param int $round The optional number of decimal digits to round to. default 0 means not round
 * @return float Random float value
 */
function float_rand($Min, $Max, $round=0){
    //validate input
    if ($min>$Max) { $min=$Max; $max=$Min; }
        else { $min=$Min; $max=$Max; }
    $randomfloat = $min + mt_rand() / mt_getrandmax() * ($max - $min);
    if($round>0)
        $randomfloat = round($randomfloat,$round);

    return $randomfloat;
}

回答by Nandu

Try this

尝试这个

// Generates and prints 100 random number between 0.0 and 1.0 
$max = 1.0;
$min = 0.0;
for ($i = 0; $i < 100; ++$i)
{
    print ("<br>");
    $range = $max - $min;
    $num = $min + $range * (mt_rand() / mt_getrandmax());    
    $num = round($num, 2);
    print ((float) $num);
}

回答by Oliver Charlesworth

Cast to float*and divide by getrandmax().

转换为浮动*并除以getrandmax()



* It seems that the cast is unnecessary in PHP's arbitrary type-juggling rules. It would be in other languages, though.* 在 PHP 的任意类型杂耍规则中,似乎不需要强制转换。不过,它会是其他语言。