如何在PHP中操纵随机数?

时间:2019-05-06 21:59:46

标签: php random

我在这里阅读了很多论坛和问题,但是找不到正确的解决方案。

因此,你们中的大多数人(我希望)都知道freebitco.in,您可以每1小时赢得一次比特币。有下表。如果您在0-9885之间滚动一个数字,您将获得以下支付等。但是,将数字大于9885进行滚动的机会非常小。例如,更改为使数字大于 9885 为5%,大于 9985 -1%,大于 9993 -0.001%, 10k -不可能。那么,我如何拥有这样的脚本?

enter image description here

2 个答案:

答案 0 :(得分:2)

您无需影响rand(),只需获取一个随机数并针对范围测试其值即可。

$val = mt_rand(0, 10000);
if ($val <= 9885) {
    $payout = 0.00000035;
} elseif ($val <= 9985) {
    $payout = 0.00000351;
} elseif ($val <= 9993) {
    $payout = 0.00003515;
} ...

答案 1 :(得分:0)

我不确定您如何计算获胜者。但是,您可能只想设计一个数组,并在数组中使用随机数和查找。

您可能不想再有概率了,但是如果愿意,您可以在数组中添加一个属性,然后在if语句或else中进行任何数学运算。

$lookup = array(
    "0" => array(
        "luck" => 9885,
        "payout" => 0.00000035,
        "probability" => 0.5,
    ),
    "1" => array(
        "luck" => 9886,
        "payout" => 0.00000351,
        "probability" => 0.3,
    ),
    "2" => array(
        "luck" => 9993,
        "payout" => 0.00003515,
        "probability" => 0.1,
    ),
    "3" => array(
        "luck" => 9997,
        "payout" => 0.00035149,
        "probability" => 0.09,
    ),
    "4" => array(
        "luck" => 9999,
        "payout" => 0.00351494,
        "probability" => 0.009,
    ),
    "5" => array(
        "luck" => 9999,
        "payout" => 0.03514939,
        "probability" => 0.001,
    ),
);

BitocoinPayout(mt_rand(0, 10000), $lookup);

function BitocoinPayout($luck_number, $lookup)
{

    foreach ($lookup as $value) {
        if ((int) $luck_number < (int) $value["luck"]) {
            // or do other math
            echo "YAAAY! You just won Stephanie Kostova's jackpot of " . $value["payout"] . " Bitcoin! ";
            break;
        } else {
            continue;
            // or do other math
        }
    }
}

输出

YAAAY! You just won Stephanie Kostova's jackpot of 3.5E-7 Bitcoin!