samedi 16 février 2019

PHP - Generating a prize based on percentages not working

$prizes = array (
        '1' => 5,
        '2' => 25, 
        '3' => 30, 
        '4' => 40
        );

    $rand = mt_rand (0*10, 100*10) / 10;

if ($rand <= $prizes['1']) {
            $prize = '1';
            }else{
            if ($rand <= $prizes['2']) {
                $prize = '2';
                }else{
                if ($rand <= $prizes['3']) {
                    $prize = '3';
                    }else{
                    if ($rand <= $prizes['4']) {
                        $prize = '4';
                        }else{
}}}}

I am trying to make a prize system where the chance of winning is 100%, but for some reason it always seems to be a loss most of the time.

This is obviously because if it's above 40% then it will always be a loss.

But the numbers all add up to 100, so I want at least 1 prize to be chosen, I am really at a loss here as to what I do.

There is a 60% chance of a loss with this code I wrote.

How else can I look at this, I have been thinking for hours on how I can do it.

I hope someone can provide some insight here for me!




Aucun commentaire:

Enregistrer un commentaire