简体   繁体   中英

PHP random/percentage chance to do one of the options

I'm looking to have a link that if a user presses it generates a random number, that number is then compared to a bunch of if statements to do something eg something like this:

$value = rand(1,1000);

Then if statements:

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

Each random click/number will only relate to one of the if statements, so only one output should happen.

This doesn't seem the most logical/simplest way to do this. Unsure what the best method would be here. Could anyone point me in the right direction of other methods to achieve something like this? Thanks!

You could use a switch statement like this:

$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;
}

but this looks very much like your code.

You could do the "something" inside functions and then call a random function:

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

This seems easy to expand. But I still think your goals are not very clear.

So what you are after is thresholds with callbacks. So what you need to build is an array of thresholds that have a callback. Then when you generate your random number you just iterate through the thresholds and compare your random number to the threshold value.

 $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;
   }

You liked the function idea, but you need varying odds. That can be done with a lookup array. So we still have these functions:

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

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

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

but now we use a lookup array like this:

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

The total sum of that array is 9, and something2 has a 1 in 9 chance of occurring, something1 a 1 in 3 chance and something3 a 5 in 9 chance.

Now we still need to call a function using these chances, that can be done this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM