A Classic Lottery Algorithm

Define the prize probability array

private $innerRate = array(
'1' => 1,
'2' => 200,
'3' => 500,
'4' => 100000,
'5' => 300000,
'6' => 1693000,
);

Call the function

1.$i = $this->getRand($this->innerRate); 2.Handle the result based on the returned $i; either if or switch works.

if($i==1)
{
	echo "一等奖";
}else{
	echo "二等奖";
}

Base function

private function getRand($proArr)
{
    $result = '';
    // 概率数组的总概率精度
    $proSum = array_sum($proArr);
    // 概率数组循环
    foreach ($proArr as $key => $proCur) {
        $randNum = mt_rand(1, $proSum); // 抽取随机数
        if ($randNum <= $proCur) {
            $result = $key; // 得出结果
            break;
        } else {
            $proSum -= $proCur;
        }
    }
    unset($proArr);
    return $result;
}
Stay updated

Get weekly research updates, market signals, and listing intelligence — follow along on Telegram or X.