Draw with probability with array

Asked

Viewed 18 times

0

Hello, good night I’m trying to make a draw with odds on each item, I’m organizing each item of the draw into an array, example:

$premios = [
'Iphone 8' =>      0.5,
'Kit Maquiagem' => 10,
'Botas de Couro' => 25,
'Fone' => 35,
'Teclado Gamer' => 40,
'Capinha' => 50,
];

and I’m treating it this way

$x = rand(0, 100);
foreach ($premios as $premio => $probability) {
$x -= $probability;
if ($x <= 0) {
echo $premio;
break;
}
}

But you’re not giving me back any results. Can anyone think of how to improve this code?

  • and the sum of those probabilities there passing from 100?

  • @Vmp help ai d=

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.