[英]Php random number generate
My task: 我的任务:
Generate random numbers between 1 and 20, to 1 decimal place. 生成1到20之间的随机数,小数点后1位。
However my issue as simple as mt_rand. 但是我的问题就像mt_rand一样简单。 I want most of the numbers generated to be lower around 0.5 - 4.5 with the occasional number being between 4.5-10 and very rarely say once every 12-20 hours being between 10-20. 我希望生成的大部分数字在0.5到4.5之间较低,偶尔的数字在4.5-10之间,很少说每12-20小时一次在10-20之间。
I've been using the following but have no idea where to go from. 我一直在使用以下,但不知道从哪里去。 I am a very basic self-taught programmer. 我是一个非常基本的自学成才的程序员。
$min = 1;
$max = 20;
$suisse_interest = mt_rand ($min*10, $max*10) / 10
Maybe if I briefly explain why I want this it may help.. 也许如果我简单地解释为什么我想要它可能有帮助..
I own an online game and want to add 3 "banks" with each bank generating different interests each hour. 我拥有一个在线游戏,并希望每个小时增加3个“银行”,每个小时产生不同的兴趣。 Most of the time I want it low, but sometimes higher and very rarely very high (15-20%). 大多数时候我想要它低,但有时更高,很少很高(15-20%)。
With the above code the random number goes too high to often. 使用上面的代码,随机数经常变得太高。
Any help with this is greatly appreciated! 非常感谢任何帮助!
You need an exponential calculation. 你需要一个指数计算。 If you use a function similar to the following function, the probability for low numbers increases. 如果使用类似于以下函数的函数,则低数字的概率会增加。 Of course you need to adapt the numbers a bit to provide an output suiting your needs. 当然,您需要调整一些数字以提供适合您需求的输出。
$i = 0;
while($i<30) {
$i++;
$rand = mt_rand(0, 7000) / 100; // 0.0 ~ 70.0
// This is the most important line:
$output = round( 20*(pow(0.95,$rand)) , 1);
echo "$output ";
}
Sample output: 样本输出:
1.8 4.3 2.6 5.5 3.7 15.5 1.6 0.6 0.6 1.6 5.8 1.3 6.1 3.2 0.8 1.7 14.7 7.9 1.3 10.3 5.5 12.6 1.5 8.4 1.5 0.9 13.3 5.8 7.5 1.7
As you see, mostly smaller number are printed. 如您所见,打印的数字通常较小。
The probability to get 20
is around 1.4%
in my code whereas the probability to get a number smaller than 5
is around 78%
在我的代码中获得20
的概率约为1.4%
,而获得smaller than 5
的数字的概率约为78%
Try this.The probability to 1.0~4.5 is around 96%, 4.5~10.0 is around 2%, and 10.0~20.0 is around 2%. 试试这个.1.0~4.5的概率约为96%,4.5~10.0约为2%,10.0~20.0约为2%。
<?php
// 1.0~4.5 96%
// 4.5~10.0 2%
// 10.0~20.0 2%
function fun() {
$num = mt_rand(1, 100);
if ($num > 0 && $num <= 96) {
$return = mt_rand(10, 45) / 10; // 96%
} else if ($num > 96 && $num <= 98) {
$return = mt_rand(45, 100) / 10; // 2%
} else {
$return = mt_rand(100, 200) / 10; // 2%
}
return sprintf("%01.1f",$return);
}
echo fun();
?>
This is not a PHP-specific problem. 这不是特定于PHP的问题。
What you need is a non-linear probability law, that you can then implement in PHP. 您需要的是一个非线性概率法,然后您可以在PHP中实现。
If you want something centered around an average value, the ideal would be a gaussian aka normal distribution , but computing it requires various complicated tricks, most of them being optimized for rapid generation at the cost of increasing complexity. 如果你想要一个以平均值为中心的东西,那么理想的是高斯即正态分布 ,但计算它需要各种复杂的技巧,其中大多数都是为了快速生成而优化,但代价是增加了复杂性。
If you generate only a few values each hour, performance will not be an issue. 如果每小时只生成几个值,则性能不会成为问题。
A reasonable approximation would be to sum 3 or 4 random variables, taking advantage of the central limit theorem . 合理的近似是利用中心极限定理对3或4个随机变量求和。
Summing random values between 0 and twice your middle rate will create an approximation of a gaussian centered around your middle value. 将中间速率的0到2之间的随机值相加将创建以中间值为中心的高斯近似值。 You can then clamp values inferior to the middle point if you don't want low rates. 如果您不想要低费率,则可以将值限制在中间点以下。 The net result would be 50% chances of getting middle rate and a steadily decreasing chance to get up to twice that value. 最终结果是获得中间率的几率为50%,并且获得两倍该值的机会稳步下降。
An increasing number of sums will "narrow" the curve, making it less likely to get a high value. 越来越多的总和将“缩小”曲线,使其不太可能获得高价值。
for instance: 例如:
define ("INTEREST_MEAN", 10);
define ("INTEREST_SPREAD", 5);
function random_interest ()
{
$res = 0;
for ($i = 0 ; $i != INTEREST_SPREAD ; $i++) $res += mt_rand(0, 2*INTEREST_MEAN);
$res /= INTEREST_SPREAD; // normalize the sum to get a mean-centered result
$res = max ($res, INTEREST_MEAN); // clamp lower values
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.