0

So I have an array of possible combinations:

$faces = array ('Bar', 'Cherry', 'Lemon', 'Seven', 'DoubleBar', 'TripleBar');

And an array of payouts

$payouts = array (
'Bar|Bar|Bar' => '5', 
'Double Bar|Double Bar|Double Bar' => '10',
'Triple Bar|Triple Bar|Triple Bar' => '15', 
'Cherry|Cherry|Cherry' => '20',
'Seven|Seven|Seven' => '70',
'Seven|Any|Any' => '70',
'Diamond|Diamond|Diamond' => '100',
);

And an array for every wheel (3 in total), how can I make weighted random combinations with Seven|Any|Any working properly?

I know I can just create two arrays 6^3 in size one representing the weights and the other array representing every possible combination and using something like this script, but isn't there a shorter, more efficient way?

function weighted_random_simple($values, $weights){
    $count = count($values);
    $i = 0;
    $n = 0;
    $num = mt_rand(0, array_sum($weights));
    while($i < $count){
        $n += $weights[$i];
        if($n >= $num){
            break;
        }
        $i++;
    }
    return $values[$i];
}
Madmadmax
  • 79
  • 3
  • 10

1 Answers1

1

How about an array that correlates counts of possible results, rather than actual positions?

e.g. you do a run, get something like A/A/B, so you've got 2 A's, and look in the table

$payouts = array(
   2 => array('Bar' => 10, 'Double Bar' => 20, etc...)
   3 => array('Diamond' => 100, 'Bar' => 40, etc...)
);
Marc B
  • 356,200
  • 43
  • 426
  • 500