简体   繁体   English

php 中的随机加权硬币翻转

[英]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.我正在尝试在 php 中创建一个硬币翻转算法,该算法根据硬币翻转的赌注金额加权。

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.下注 1 是 50/50 的机会,但任何事情都会慢慢变得越来越不可能获胜。

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.这适用于没有 IRL 值的积分余额系统,仅用于我托管的游戏服务器。 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.结果应该是更高赌注获胜的机会更低,但是,即使使用我编写的代码,它似乎仍然是准确的 50/50 机会。

this seems to work for my needs这似乎适合我的需要

<?PHP
$wager = $_GET['wager'];
$win = $wager *2;

if ($wager < 10) {
    die("You must wager at least 10 credits."); 
}

if ($_GET['side'] == 0) {
$values[0] = 0;
$weights[0] = 5;    
$values[1] = 1;
$weights[1] = $wager*2; 
} else {
    $values[0] = 0;
$weights[0] = $wager*2; 
$values[1] = 1;
$weights[1] = 5;    

}

$side = weighted_random_simple($values, $weights);


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 ($side  == 0) {
    echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=TAILS" />';
} else {
echo '<img src="https://static.pdglobal.app/?sid=files_go&ID=HEADS" />';
}

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());
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM