Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP随机/完成其中一个选项的概率百分比_Php_Logic - Fatal编程技术网

PHP随机/完成其中一个选项的概率百分比

PHP随机/完成其中一个选项的概率百分比,php,logic,Php,Logic,我希望有一个链接,如果用户按下它,它会生成一个随机数,然后将该数与一组if语句进行比较,以执行以下操作: $value = rand(1,1000); $numberOfSomethings = 5; switch (rand(1,$numberOfSomethings)) { case 1: // do something number 1 break; case 2: // do something number 2

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

$value = rand(1,1000);
$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;
}
$events = ['something1' => 3,
           'something2' => 1,
           'something3' => 5];
然后,如果声明:

if ($value >= 1 && $value <= 10) {
// do something
} elseif ($value >= 11 && $value <= 20) {
// do something else
} .... etc
如果($value>=1&&$value=11&&$value您可以使用这样的:

$value = rand(1,1000);
$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;
}
$events = ['something1' => 3,
           'something2' => 1,
           'something3' => 5];
但这看起来很像你的代码

您可以在函数中执行“something”,然后调用一个随机函数:

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
这似乎很容易扩展。但我仍然认为您的目标不是很明确。

您可以使用以下方法:

$value = rand(1,1000);
$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;
}
$events = ['something1' => 3,
           'something2' => 1,
           'something3' => 5];
但这看起来很像你的代码

您可以在函数中执行“something”,然后调用一个随机函数:

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

这似乎很容易扩展。但我仍然认为您的目标不是很明确。

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

 $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;
   }
$number=rand(11000);
$thresholds=[
[10,函数(){}],
[20,函数(){}],
[30,函数(){}]
];
//现在我们可以用随机数循环通过每个阈值,因为我们只需要检查每个阈值的最后一个最高值就知道我们通过了阈值。
foreach($index=>$threshold的阈值){
//我们需要首先检查这是否是最后一个阈值,因此我们需要知道这是什么。
如果(数组\u键\u最后($threshold)==$index){
$threshold[1]();
打破
}
//这里我们检查这个数字是否大于阈值,也是否小于下一个阈值,然后调用它的回调函数。
elseif($number>=$threshold[0]&&$number<$thresholds[$index+1][0]){
$threshold[1]();
打破
}

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

 $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;
   }
$number=rand(11000);
$thresholds=[
[10,函数(){}],
[20,函数(){}],
[30,函数(){}]
];
//现在我们可以用随机数循环通过每个阈值,因为我们只需要检查每个阈值的最后一个最高值就知道我们通过了阈值。
foreach($index=>$threshold的阈值){
//我们需要首先检查这是否是最后一个阈值,因此我们需要知道这是什么。
如果(数组\u键\u最后($threshold)==$index){
$threshold[1]();
打破
}
//这里我们检查这个数字是否大于阈值,也是否小于下一个阈值,然后调用它的回调函数。
elseif($number>=$threshold[0]&&$number<$thresholds[$index+1][0]){
$threshold[1]();
打破
}

您喜欢函数的想法,但您需要不同的几率。这可以通过查找数组来完成。因此我们仍然有以下函数:

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

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

function something3()
{
    // do something number 3
}
但现在我们使用如下查找数组:

$value = rand(1,1000);
$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;
}
$events = ['something1' => 3,
           'something2' => 1,
           'something3' => 5];
该数组的总和为9,
something2
发生的概率为9分之一,
something1
1/3和
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;
    }
}

您喜欢函数的想法,但您需要不同的可能性。这可以通过查找数组来完成。因此,我们仍然有以下函数:

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

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

function something3()
{
    // do something number 3
}
但现在我们使用如下查找数组:

$value = rand(1,1000);
$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;
}
$events = ['something1' => 3,
           'something2' => 1,
           'something3' => 5];
该数组的总和为9,
something2
发生的概率为9分之一,
something1
1/3和
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;
    }
}

这完全取决于你的目标是什么,而你并没有真正告诉我们。例如:“要做的事”是相互依存的还是独立的?其中有多少是“要做的事”必须做什么?数字是否会变化?@KIKOSoftware啊,对不起!只会执行其中一个。因此用户随机选择一个数字。这个数字是600。这相当于一个特定的if语句/做某事。这取决于你的目标是什么,而你并没有真正告诉我们。例如:“要做的事情”是什么相互依存还是相互独立?这些“东西”有多少必须这样做吗?数字是否会变化?@KIKOSoftware啊,对不起!只会执行其中一个。因此用户随机选择一个数字。这个数字是600。这相当于一个特定的if语句/做一些事情。谢谢!函数可能是个好主意。对我想要的东西进行一点扩展。基本上是一个“洞穴”用户正在输入。当他们输入时,他们会滚动一个在洞穴内发生的随机事件。我希望能够有一些发生概率较高的事件,一些发生概率较低的事件。因此,用户进入洞穴,他们滚动一个500,500在if语句中,比如说给用户一些黄金。希望这更有意义!是的,这有意义。所以t每个事件发生的几率都不一样。这改变了一切。很明显,在我的例子中,几率都是一样的。这将有很多案例需要编写。谢谢!函数可能是个好主意。来扩展一下我所追求的。它基本上是一个“洞穴”用户正在输入。当他们输入时,他们会滚动一个在洞穴内发生的随机事件。我希望能够有一些发生概率较高的事件,一些发生概率较低的事件。因此,用户进入洞穴,他们滚动一个500,500在if语句中,比如说给用户一些黄金。希望这更有意义!是的,这有意义。所以t每个事件发生的几率都不一样。这改变了一切。很明显,在我的例子中,几率都是一样的。这将是很多案例要写。