【发布时间】:2020-01-21 21:36:16
【问题描述】:
我正在尝试在 php 中创建一个抛硬币算法,该算法根据抛硬币的赌注金额进行加权。
例如,我希望下注较高的用户获胜的机会较低。下注 1 是 50/50 的机会,但任何事情都会慢慢变得越来越不可能获胜。
我已经搞砸了很多,似乎真的无法将硬币翻转编码为这种行为。
这是一个没有 IRL 值的积分余额系统,仅用于我托管的游戏服务器。使用当前系统,用户可以通过自动点击器轻松增加他们的余额,因为它是双翻转或无翻转。
这是我现在正在尝试的,但效果不是很好
<?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());
}
?>
结果应该是较高赌注获胜的机会较低,但是,即使使用我编写的代码,它似乎仍然是准确的 50/50 机会。
【问题讨论】:
-
要对抛硬币进行加权,请从 0 到 99 中选择一个数字,并测试该数字是大于还是小于加权百分比。
-
那么您只需要决定如何将投注金额转换为百分比即可。