簡體   English   中英

php 中的隨機加權硬幣翻轉

[英]Random weighted coin flip in php

我正在嘗試在 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 機會。

這似乎適合我的需要

<?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