14

I understand that this question has likely been asked, but I don't understand the prior questions enough to know if they do what I want.

$fruits = array('20' => 'apple', '40' => 'orange', '40' => 'pear');

The keys are percentages of the chance of value getting picked. I would like to mt_rand() a number between 0 and 99 and return a value from $fruits based on those percentages.

It's very possible I'm so confused because I don't know how to explain what I'm looking for.

Thank you in advance for the help.

Edit: want a random value from $fruits, based on these chances:

I want a 40% chance of getting an orange, a 40% chance of getting a pear, and an 80% chance of getting an apple.

Edit: To further clarify, since either a lot of the answers got it wrong, (or I just don't understand their code), I needed a result regardless of what number I pick, not just 20, 40, or 40.

Drazisil
  • 3,070
  • 4
  • 33
  • 53
  • Do you mean you will have 100 fruits in your array? – jtheman Sep 24 '12 at 19:31
  • I don't think this does what you want at all...you're repeating the **key** '40'. Perhaps you meant for your key/value pairs to be reversed? If so, your array would look like `$fruits = array('apple' => '20', 'orange' => '40', 'pear' => '40');`. – Travesty3 Sep 24 '12 at 19:31
  • No, I want a 40% chance of getting an orange, a 40% chance of getting a pear, and an 80% chance of getting an apple. I'll add that. – Drazisil Sep 24 '12 at 19:33
  • 3
    @Drazisil: Does not compute... – Travesty3 Sep 24 '12 at 19:33
  • @jthemanjtheman Yes, I guess it does. an array of 100 values, filled in with those percentages. Although I'm not sure how to build the array, which is why I just have the first one. – Drazisil Sep 24 '12 at 19:45
  • 1
    @Drazisil: I think you mean that you want a 20% chance of getting an apple. 40 + 40 + 80 = 160%. Right? – Travesty3 Sep 24 '12 at 19:49
  • Check https://github.com/ihor/Nspl#weightedchoicearray-weightpairs – Ihor Burlachenko Dec 25 '15 at 16:41

4 Answers4

23

First of all, your array only has two items (apple and pear) because pear overwrites orange due to having the same key. So use the opposite order. Also, don't put quotes around the integers:

$fruits = array('apple' => 20, 'orange' => 40, 'pear' => 40);

You should choose a random number, then compare each weight of the array plus the sum of the previous weights until you get a match:

$rand = rand(1,100);
$sum = 0;
$chosenFruit;

foreach ($fruits as $f=>$v) {
    $sum += $v;
    if ( $sum >= $rand ) {
        $chosenFruit = $f;
        break;
    }
}

echo "We have chosen the {$chosenFruit}!";

You could even make the procedure more resilient by replacing the value of 100 in the rand() function with a calculated sum of the $fruits array values.

dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • 1
    this is a more efficient answer than the selected one. As @dotancohen points out. – Yehosef Nov 17 '14 at 13:07
  • A more general solution would be not to have rand fixed from 1 to 100, but to the sum of all weights. The problematic case is when all of them have for example 33% percent chance. It would be more precise to pick rand from 1 to 99 (i.e. the sum of all weights). – Vladan Aug 28 '19 at 12:12
  • This seems to be in agreement with https://stackoverflow.com/a/11872928/470749 and was helpful. +1 – Ryan Jan 20 '20 at 17:59
11

I think something like this will do what you want:

sample

(click the submit button multiple times on the sample to get the code to re-execute)

$fruits = array('apple' => '20', 'orange' => '40', 'pear' => '40');

$newFruits = array();
foreach ($fruits as $fruit=>$value)
{
    $newFruits = array_merge($newFruits, array_fill(0, $value, $fruit));
}

$myFruit = $newFruits[array_rand($newFruits)];

This creates an array ($newFruits), which is a numerically-indexed array with 100 elements. 20 of those elements are 'apple', 40 are 'orange', and 40 are 'pear'. Then we select a random index from that array. 20 times out of 100 you will get 'apple', 40 times out of 100 you will get 'orange', and 40 times out of 100 you will get 'pear'.

Travesty3
  • 14,351
  • 6
  • 61
  • 98
  • 1
    @Drazisil: It's working fine for me...hit the submit button over and over again. Refreshing doesn't seem to re-execute the code. – Travesty3 Sep 24 '12 at 19:41
  • 1
    It appears to work exactly as I wanted...thank you very much for the help. – Drazisil Sep 24 '12 at 20:00
  • 3
    This is going to be a memory hog. I would not recommend this type of code on a page that is accessed frequently by multiple users. If it is simply for a single-use project, though, it should be fine. – dotancohen Sep 24 '12 at 20:08
  • How is an array of 100 integers a memory hog? – rodrigo-silveira Jul 15 '15 at 12:40
  • I agree that it is not as efficient as dotancohen's answer. I also agree that it's no big deal for 100 integers, but thinking on a bigger scale, it doesn't necessarily have to be just 100 integers. You could go up to any number you want. And if that number is significantly higher, and it's used frequently by multiple users, it could start to cause noticeable issues. I would agree that dotancohen's answer does not have the scalability issue, although (IMO) slightly more difficult to comprehend. – Travesty3 Jul 15 '15 at 13:30
1

There are multiple roads to go down with this one and they pretty much depend on your data. Using percentages, 0 - 100, you could have 100 items in your array that each map to a percentage, or you can have a distribution with assigned values (much like the array in your sample).

Assuming the former, you can use:

$key = mt_rand(0, 100);
$fruit = isset($fruits[$key]) ? $fruits[$key] : false;

The above could also work with a distribution, to check if a fruit "exists", but normally you would want "if the percentage is <= the value, use this fruit". Something like that can be done with:

$key = mt_rand(0, 100);
$fruit = false;
foreach ($fruits as $percent => $value) {
    if ($percent <= $key) {
        $fruit = $value;
        break;
    }
}

Now, the fun part. What if you wanted any of the fruits that have a percentage above the value? You can modify the above to store each fruit that matches into an array and then pick a random value from that array:

$key = mt_rand(0, 100);
$matches = array();
foreach ($fruits as $percent => $value) {
    if ($percent <= $key) {
        $matches[] = $value;
    }
}
$select = mt_rand(0, count($matches) - 1);
$fruit = $matches[$select];
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
0
  $key = rand(1,98); // rand() is inclusive of the range, hence 1-98.
  foreach( $fruits as $k => $v){
  if( $k == $key) {
    echo 'Chosen fruit is:' . $v; 
    break;
    } 
  }
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17