金字塔状分布的调整函数
在 这个问题中,我得到了编写 PHP 的帮助给出类似金字塔的分布的函数:
function getRandomStrength($min, $max) {
$ln_low = log($min, M_E);
$ln_high = log($max, M_E);
$scale = $ln_high-$ln_low;
$rand = (mt_rand()/mt_getrandmax())*$scale+$ln_low;
$value = round(pow(M_E, $rand), 1);
return $value;
}
getRandomStrenth(1.1, 9.9);
// output could be: 1.4 or 8.3 or 9.8 or 7.2 or 2.9 or ...
当我运行 50,000 次迭代并检查从 1 到 9 的数字出现的频率时,我得到以下列表:
- 1 » 26%
- 2 » 19%
- 3 » 14%
- 4 » 10%
- 5 » 9%
- 6 » 7
- % 7 » 6%
- 8 » 6%
- 9 » 4%
这就是我想要的。但现在我想稍微调整一下这个功能。较小的值应该更频繁地出现,而大的值应该更少地出现 - 这样我就会得到这样的列表:
- 1 » 28%
- 2 » 20%
- 3 » 15%
- 4 » 11%
- 5 » 9%
- 6 » 6%
- 7 » 5%
- 8 » 5%
- 9 » 2%
如您所见,我只需要稍微修改。但是我可以改变什么以使我的函数按预期运行?
我尝试了几件事(例如改变对数的底数)但这并没有改变任何东西。
In this question I got help to write a PHP function which gives a pyramid-like distribution:
function getRandomStrength($min, $max) {
$ln_low = log($min, M_E);
$ln_high = log($max, M_E);
$scale = $ln_high-$ln_low;
$rand = (mt_rand()/mt_getrandmax())*$scale+$ln_low;
$value = round(pow(M_E, $rand), 1);
return $value;
}
getRandomStrenth(1.1, 9.9);
// output could be: 1.4 or 8.3 or 9.8 or 7.2 or 2.9 or ...
When I run 50,000 iterations and check how often the numbers from 1 to 9 appear, I get the following list:
- 1 » 26%
- 2 » 19%
- 3 » 14%
- 4 » 10%
- 5 » 9%
- 6 » 7%
- 7 » 6%
- 8 » 6%
- 9 » 4%
This is what I wanted to have. But now I would like to adjust this function a bit. The smaller values should appear more often and the big values should appear less often - so that I get a list like this:
- 1 » 28%
- 2 » 20%
- 3 » 15%
- 4 » 11%
- 5 » 9%
- 6 » 6%
- 7 » 5%
- 8 » 5%
- 9 » 2%
As you can see, I just need a slight modification. But what can I change so that my function behaves as expected?
I tried several things (e.g. changing the base of the logarithm) but this did not change anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以对随机数使用 pow。
$rand = pow( mt_rand()/mt_getrandmax(), 1.2 )*$scale+$ln_low;
通过使用指数值,您可以获得更小或更小的值。
You can use pow on the random number.
$rand = pow( mt_rand()/mt_getrandmax(), 1.2 )*$scale+$ln_low;
By playing with the exponent value, you can get less or more small value.
将函数的
$scale
减少少量(恒定)似乎会生成非常接近您正在寻找的结果。您可以通过将$scale
的减少作为mt_rand()
随机生成的数字的函数来获得更准确的结果,这需要保存(mt_rand() /mt_getrandmax())
到一个变量并在$scale
上执行一些额外的数学运算。这是我的测试,你可以自己运行一下: http://codepad.viper-7.com/ssblbQ< /a>
输出
运行 #1 - 常量 = 0.5
运行 #2 - 常量 = 0.65
运行 #3 - 常量 = 0.70
Reducing the
$scale
of your function by a small (constant) amount seems to generate results pretty close to what you're looking for. You can achieve more accurate results by making this reduction of$scale
a function of the randomly generated number frommt_rand()
, which would require saving(mt_rand()/mt_getrandmax())
to a variable and performing some additional math on$scale
.Here are my tests, you can run it yourself: http://codepad.viper-7.com/ssblbQ
Output
Run #1 - Constant = 0.5
Run #2 - Constant = 0.65
Run #3 - Constant = 0.70
对于 {0..1} 中的 n,y=(x^n)-1,y 的范围为 0 到 x-1。然后,通过乘以范围并除以 (x-1),该曲线可以轻松地从 0 映射到某个最大值。如果将值 x 更改为接近 1 的值,则曲线将接近线性,并且在较大值时,曲线变得更像曲棍球棒,但仍会落在相同的范围内。
我的初始样本值 3 并不完全是您所表达的值,但您可以调整它以获得您正在寻找的分布曲线。
getRandomStrength(1.1, 9.9);
For n in {0..1}, y=(x^n)-1, y will range from 0 to x-1. That curve is then easily mapped from 0 to some max value by multiplying by the range and dividing by (x-1). If you change the value x to something near one, the curve will be nearly linear, and at large values, the curve becomes more like a hockey-stick, but will still fall in the same range.
My initial sample value of three won't be precisely what you expressed, but you can adjust it to get the distribution curve you're looking for.
getRandomStrength(1.1, 9.9);