【问题标题】:PHP random/percentage chance to do one of the optionsPHP 随机/百分比机会执行选项之一
【发布时间】:2020-05-01 08:37:05
【问题描述】:

我希望有一个链接,如果用户按下它会生成一个随机数,然后将该数字与一堆 if 语句进行比较以执行某些操作,例如:

$value = rand(1,1000);

那么 if 语句:

if ($value >= 1 && $value <= 10) {
// do something
} elseif ($value >= 11 && $value <= 20) {
// do something else
} .... etc

每个随机点击/数字只会与其中一个 if 语句相关,因此只会发生一个输出。

这似乎不是最合乎逻辑/最简单的方法。不确定这里最好的方法是什么。谁能指出我其他方法的正确方向来实现这样的目标?谢谢!

【问题讨论】:

  • 这完全取决于您的目标是什么,而您还没有真正告诉我们。例如:“要做的事情”是相互依赖的还是独立的?这些“事情”中有多少必须做?数量可以变化吗?
  • @KIKOSoftware 啊对不起!只会执行其中之一。所以用户随机选择一个数字。这个数字是 600。这相当于一个特定的 if 语句/做某事。

标签: php logic


【解决方案1】:

您可以像这样使用switch statement

$numberOfSomethings = 5;

switch (rand(1,$numberOfSomethings)) {
    case 1: 
        // do something number 1
        break;
    case 2: 
        // do something number 2
        break;
    case 3: 
        // do something number 3
        break;
    case 4: 
        // do something number 4
        break;
    case 5: 
        // do something number 5
        break;
}

但这看起来很像你的代码。

你可以在函数内部做一些“事情”,然后调用一个随机函数:

function something1()
{
    // do something number 1
}

function something2()
{
    // do something number 3
}

function something3()
{
    // do something number 3
}

$functionName = "something".rand(1,3); // choose a function
$functionName(); // and call it

这似乎很容易扩展。但我还是觉得你的目标不是很明确。

【讨论】:

  • 谢谢!这些功能可能是个好主意。扩展一下我所追求的。它基本上是用户进入的“洞穴”。当他们进入时,他们会滚动发生在洞穴内的随机事件。我希望能够有一些发生几率较高,一些发生几率较低等。所以用户进入洞穴,他们掷出 500,500 在 if 语句中,例如给用户一些金币。希望这更有意义!
  • 是的,这是有道理的。所以每个事件发生的几率是不一样的。这改变了事情。显然,在我的例子中,可能性都是一样的。
  • 要写很多案例。
【解决方案2】:

因此,您所追求的是带有回调的阈值。因此,您需要构建一组具有回调的阈值。然后,当您生成随机数时,您只需遍历阈值并将随机数与阈值进行比较。

 $number = rand(1, 1000);

 $thresholds = [
    [10,function(){}],
    [20,function(){}],
    [30,function(){}]
 ];

 //now we can loop through each threshold with the random number.  As we only need to check the last highest value of each to know we passed the threshold.

 foreach($thresholds as $index=> $threshold){
   //we need to check if this is the last threshold first so we need to know what that is.   
   if(array_key_last($threshold) === $index){
      $threshold[1]();
      break;
   }

   //here we check if the number is greater than the threshold and also less than the next threshold. then call its callback.
   elseif($number >= $threshold[0] && $number < $thresholds[$index + 1][0]){
      $threshold[1]();
      break;
   }

【讨论】:

    【解决方案3】:

    你喜欢函数的想法,但你需要不同的赔率。这可以通过查找数组来完成。所以我们还是有这些功能的:

    function something1()
    {
        // do something number 1
    }
    
    function something2()
    {
        // do something number 3
    }
    
    function something3()
    {
        // do something number 3
    }
    

    但现在我们使用这样的查找数组:

    $events = ['something1' => 3,
               'something2' => 1,
               'something3' => 5];
    

    该数组的总和为 9,something2 有 9 分之一的机会出现,something1 有 1 分之三的机会,something3 有 5 分之 9 的机会。

    现在我们仍然需要利用这些机会调用一个函数,可以这样做:

    $total  = array_sum($events);
    $number = rand(1, $total);
    $sum    = 0;
    foreach ($events as $functionName => $chance) {
        $sum += $chance;
        if ($sum >= $number) {
            $functionName();
            break;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 2019-09-24
      • 2021-06-22
      相关资源
      最近更新 更多