vendredi 20 septembre 2019

Random weighted coin flip in php

I am trying to make a coin flip algorithm in php that is weighted based on the amount gambled on the coin flip.

For example, I want a user who has a higher wager to have a lower chance of winning. With a wager of 1 being a 50/50 chance, but anything more slowly becoming less and less likely to win.

I've messed around a lot and can't really seem to code the coin flip to behave this way.

This is for a credits balance system which has no IRL value, that is used solely for game servers I host. With the current system a user can easily multiply their balance with an auto clicker, as it's a double or nothing flip.

this is what I'm trying right now, but it's not so effective

<?PHP
$choices = ["heads", "tails","tails", "heads","heads", "tails","heads", "tails", "heads"];
$result = $choices[random_int(0, count($choices)-1)];
$wager = $_GET['wager'];
$win = $wager *2;

if (!is_numeric($wager)) {
    die("You must enter a number for your wager.");
}

if ($wager > $usrdata['money']) {
    die("You don't have enough credits to make this bet.");
}

if ($result  == "tails") {
    if ($_GET['side'] == 0) {
        $newrand = rand(0, $wager);
    }
    $newrandx = rand(0, 1);
    if ($newrandx == 1 && $side == 0) {
        echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=HEADS" />';
    $side = 1;
    } else {
    if ($wager/2 >= $newrand) {
    echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=TAILS" />';
    $side = 0;
    } else {
        echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=HEADS" />';
    $side = 1;

    }
    }
} else {

    if ($_GET['side'] == 1) {
        $newrand = rand(0, $wager);
    }
        $newrandx = rand(0, 1);
    if ($newrandx == 1 && $side == 1) {
        echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=TAILS" />';
    $side = 0;
    } else {
    if ($newrand >= $wager/2) {
    echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=HEADS" />';
    $side = 1;
    } else {
    echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=TAILS" />';
    $side = 0;  

    }
    }
}

if ($side == $_GET['side']) {
    echo '<br/> You won '.$win.' credits!';
    $total = $usrdata['money'] + $win;
    mysql_query("UPDATE account SET money = '".$total."' WHERE username = '".$usrdata['username']."';") or die(mysql_error());
} else {
    echo '<br/> You lost '.$wager.' credits!';
    $total = $usrdata['money'] - $wager;
    mysql_query("UPDATE account SET money = '".$total."' WHERE username = '".$usrdata['username']."';") or die(mysql_error());
    }
?>

The result should be a lower chance of winning with a higher wager, however, it still seems to be an exact 50/50 chance even with the code I've written up.




Aucun commentaire:

Enregistrer un commentaire