金字塔状分布的调整函数

发布于 2024-12-14 04:18:08 字数 1077 浏览 2 评论 0原文

这个问题中,我得到了编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兰花执着 2024-12-21 04:18:08

您可以对随机数使用 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.

少女净妖师 2024-12-21 04:18:08

将函数的 $scale 减少少量(恒定)似乎会生成非常接近您正在寻找的结果。您可以通过将 $scale 的减少作为 mt_rand() 随机生成的数字的函数来获得更准确的结果,这需要保存 (mt_rand() /mt_getrandmax()) 到一个变量并在 $scale 上执行一些额外的数学运算。

这是我的测试,你可以自己运行一下: http://codepad.viper-7.com/ssblbQ< /a>

function getRandomStrength($min, $max) 
{
    $ln_low = log($min, M_E);
    $ln_high = log($max, M_E);
    $scale = $ln_high-$ln_low - .05; // Subtract a small constant, vary between .05 and .08
    $rand = (mt_rand()/mt_getrandmax())*$scale+$ln_low;
    $value = round(pow(M_E, $rand), 1);
    return $value;
}

$values = array_fill(1, 9, 0);
for( $i = 0; $i < 50000; $i++) 
{
    $values[ intval( getRandomStrength(1.1, 9.9)) ]++;
}

for( $i = 1; $i <= 9; $i++) 
{
    $values[ $i] /= 500; // / 50000 * 100 to get a percent
}

var_dump( $values);

输出

运行 #1 - 常量 = 0.5

array(9) {
  [1] => float(26.626) // Should be 28
  [2] => float(19.464) // Should be 20
  [3] => float(13.476) // Should be 15
  [4] => float(10.41) // Should be 11
  [5] => float(8.616) // Should be 9
  [6] => float(7.198) // Should be 6
  [7] => float(6.258) // Should be 5
  [8] => float(5.52) // Should be 5
  [9] => float(2.432) // Should be 2
}

运行 #2 - 常量 = 0.65

array(9) {
  [1] => float(26.75) // Should be 28
  [2] => float(19.466) // Should be 20
  [3] => float(13.872) // Should be 15
  [4] => float(10.562) // Should be 11
  [5] => float(8.466) // Should be 9
  [6] => float(7.222) // Should be 6
  [7] => float(6.454) // Should be 5
  [8] => float(5.554) // Should be 5
  [9] => float(1.654) // Should be 2
}

运行 #3 - 常量 = 0.70

array(9) {
  [1] => float(26.848) // Should be 28
  [2] => float(19.476) // Should be 20
  [3] => float(13.808) // Should be 15
  [4] => float(10.764) // Should be 11
  [5] => float(8.67) // Should be 9
  [6] => float(7.148) // Should be 6
  [7] => float(6.264) // Should be 5
  [8] => float(5.576) // Should be 5
  [9] => float(1.446) // Should be 2
}

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 from mt_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

function getRandomStrength($min, $max) 
{
    $ln_low = log($min, M_E);
    $ln_high = log($max, M_E);
    $scale = $ln_high-$ln_low - .05; // Subtract a small constant, vary between .05 and .08
    $rand = (mt_rand()/mt_getrandmax())*$scale+$ln_low;
    $value = round(pow(M_E, $rand), 1);
    return $value;
}

$values = array_fill(1, 9, 0);
for( $i = 0; $i < 50000; $i++) 
{
    $values[ intval( getRandomStrength(1.1, 9.9)) ]++;
}

for( $i = 1; $i <= 9; $i++) 
{
    $values[ $i] /= 500; // / 50000 * 100 to get a percent
}

var_dump( $values);

Output

Run #1 - Constant = 0.5

array(9) {
  [1] => float(26.626) // Should be 28
  [2] => float(19.464) // Should be 20
  [3] => float(13.476) // Should be 15
  [4] => float(10.41) // Should be 11
  [5] => float(8.616) // Should be 9
  [6] => float(7.198) // Should be 6
  [7] => float(6.258) // Should be 5
  [8] => float(5.52) // Should be 5
  [9] => float(2.432) // Should be 2
}

Run #2 - Constant = 0.65

array(9) {
  [1] => float(26.75) // Should be 28
  [2] => float(19.466) // Should be 20
  [3] => float(13.872) // Should be 15
  [4] => float(10.562) // Should be 11
  [5] => float(8.466) // Should be 9
  [6] => float(7.222) // Should be 6
  [7] => float(6.454) // Should be 5
  [8] => float(5.554) // Should be 5
  [9] => float(1.654) // Should be 2
}

Run #3 - Constant = 0.70

array(9) {
  [1] => float(26.848) // Should be 28
  [2] => float(19.476) // Should be 20
  [3] => float(13.808) // Should be 15
  [4] => float(10.764) // Should be 11
  [5] => float(8.67) // Should be 9
  [6] => float(7.148) // Should be 6
  [7] => float(6.264) // Should be 5
  [8] => float(5.576) // Should be 5
  [9] => float(1.446) // Should be 2
}
漆黑的白昼 2024-12-21 04:18:08

对于 {0..1} 中的 n,y=(x^n)-1,y 的范围为 0 到 x-1。然后,通过乘以范围并除以 (x-1),该曲线可以轻松地从 0 映射到某个最大值。如果将值 x 更改为接近 1 的值,则曲线将接近线性,并且在较大值时,曲线变得更像曲棍球棒,但仍会落在相同的范围内。

我的初始样本值 3 并不完全是您所表达的值,但您可以调整它以获得您正在寻找的分布曲线。

function getCustomStrength($min, $max, $x_val, $base) {
  $logmax = $base-1;
  $range = $max-$min;
  return (pow($base,$x_val)-1)*($range/($base-1))+$min;
}

function getRandomStrength($min, $max) {
  $rand = mt_rand()/mt_getrandmax();
  $base = 3.0;
  return getCustomStrength($min, $max, $rand, $base);
}

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.

function getCustomStrength($min, $max, $x_val, $base) {
  $logmax = $base-1;
  $range = $max-$min;
  return (pow($base,$x_val)-1)*($range/($base-1))+$min;
}

function getRandomStrength($min, $max) {
  $rand = mt_rand()/mt_getrandmax();
  $base = 3.0;
  return getCustomStrength($min, $max, $rand, $base);
}

getRandomStrength(1.1, 9.9);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文