2
头图

🎈 Random draw

  • Random lottery is of course the fairest lottery, that is, when the user draws the lottery, the lottery result is randomly returned
  • This kind of result is completely random and not controlled by human beings. Winning the lottery is all based on luck.
  • First define the prizes owned by the prize pool. After the user enters the lottery, they will randomly return to the prizes in the prize pool.
 <?php
$prize = ['60寸大彩电', 'iphone13', '戴森吸尘器', '索尼微单', 'VR眼镜', '谢谢参与'];

// 当有用户进来抽奖,进行随机抽奖
shuffle($prize);

// 抽奖结果
print_r($prize[0]);

🎈 Probability Draw

  • Probability lottery is actually setting a probability for prizes. Generally, high-value prizes will have a very low probability of winning.
  • This kind of lottery is also a kind of random lottery, but there is no random lottery without probability control like the above.
  • It takes a lot of luck to win the grand prize, and most people will draw low-value prizes
 <?php
$prize = [
    ['name' => '60寸大彩电', 'chance' => 100],
    ['name' => 'iphone13', 'chance' => 900],
    ['name' => '戴森吸尘器', 'chance' => 1000],
    ['name' => '索尼微单', 'chance' => 2000],
    ['name' => 'VR眼镜', 'chance' => 3000],
    ['name' => '谢谢参与', 'chance' => 3000]
];

// 概率重组
$chance = 0;
foreach ($prize as &$item) {
    $chance += $item['chance'];
    $item['chance'] = $chance;
}

// 随机抽奖
$rand = mt_rand(1, 10000);

$result = [];
foreach ($prize as $_k => $_v) {
    if ($_k == 0) {
        if ($rand > 0 && $rand <= $_v['chance']) {
            $result = $_v;
            break;
        }
    } else {
        if ($rand > $prize[$_k - 1]['chance'] && $rand <= $_v['chance']) {
            $result = $_v;
            break;
        }
    }
}

// 抽奖结果
echo json_encode(compact('rand', 'result'));

🎈 Default draw

  • The default lottery is a common lottery method for the annual meeting. The company aims to reward those who have made significant contributions to the company this year.
  • Choose to give designated prizes to those who will be given a lottery at the annual meeting
  • Not only can it bring encouragement to those people, it is more to strengthen the cohesion of the company
  • In this lottery mode, the prize has been bound to the default person earlier.
  • Only when the designated person comes in can the prize be drawn. Everyone else is thankful for participating, but the user does not know that this is a default
 <?php
$prize = [
    ['name' => '60寸大彩电', 'winners' => ['张三']],
    ['name' => 'iphone13', 'winners' => ['李四', '王五']],
    ['name' => '戴森吸尘器', 'winners' => ['飞兔小哥']],
    ['name' => '索尼微单', 'winners' => ['李六']],
    ['name' => 'VR眼镜', 'winners' => ['小明']]
];

// 开始抽奖,这里假如飞兔小哥过来抽
// 这里的用户也可以是用户唯一标识
$user = '飞兔小哥';

$result = '谢谢参与';
foreach ($prize as $item) {
    if (in_array($user, $item['winners'])) {
        $result = $item['name'];
        break;
    }
}

print_r('获得的奖品:' . $result);

极客飞兔
1.2k 声望649 粉丝