平时偶尔也会买买彩票玩,但是感觉真的很少很少概率才会中奖,于是今天突发奇想,自己写个在线模拟双色球开奖和选号的程序,模拟一下中奖几率,以及过过干瘾,哈哈。
废话不多说,直接上代码
<?php
class DoubleColorBall {
private $maxWinningAmount = 0; // 记录中奖最大金额
private $maxWinningNumber; // 记录中奖最大号码
// 生成指定数量的随机号码
public function generateRandomNumbers($count) {
$numbers = [];
for ($i = 0; $i < $count; $i++) {
$redBalls = [];
// 随机生成6个不重复的红球号码(1-33)
while (count($redBalls) < 6) {
$ball = mt_rand(1, 33);
if (!in_array($ball, $redBalls)) {
$redBalls[] = $ball;
}
}
sort($redBalls); // 对红球号码进行排序
$blueBall = mt_rand(1, 16); // 随机生成一个蓝球号码(1-16)
$numbers[] = array(
'redBalls' => $redBalls,
'blueBall' => $blueBall
);
}
return $numbers;
}
// 计算购买成本
public function calculateCost($numbers, $pricePerTicket) {
$count = count($numbers);
$cost = $count * $pricePerTicket;
return $cost;
}
// 检查指定号码是否中奖,并返回中奖金额
public function checkWinning($selectedNumber, $winningNumber) {
$selectedRedBalls = $selectedNumber['redBalls'];
$selectedBlueBall = $selectedNumber['blueBall'];
$winningRedBalls = $winningNumber['redBalls'];
$winningBlueBall = $winningNumber['blueBall'];
$matchedRedBalls = array_intersect($selectedRedBalls, $winningRedBalls);
$matchedRedCount = count($matchedRedBalls);
$matchedBlueBall = ($selectedBlueBall == $winningBlueBall);
if ($matchedRedCount == 6 && $matchedBlueBall) {
return 10000000;
} elseif ($matchedRedCount == 6) {
return 500000;
} elseif ($matchedRedCount == 5 && $matchedBlueBall) {
return 3000;
} elseif ($matchedRedCount == 5 || ($matchedRedCount == 4 && $matchedBlueBall)) {
return 200;
} elseif ($matchedRedCount == 4 || ($matchedRedCount == 3 && $matchedBlueBall)) {
return 10;
} elseif ($matchedBlueBall) {
return 5;
} else {
return 0;
}
}
// 计算中奖金额
public function calculateWinnings($numbers, $winningNumber) {
$totalWinnings = 0;
foreach ($numbers as $number) {
$winnings = $this->checkWinning($number, $winningNumber);
if ($winnings > $this->maxWinningAmount) {
$this->maxWinningAmount = $winnings;
$this->maxWinningNumber = $number;
}
$totalWinnings += $winnings;
}
return $totalWinnings;
}
// 获取中奖最大号码
public function getMaxWinningNumber() {
return $this->maxWinningNumber;
}
// 获取中奖最大金额
public function getMaxWinningAmount() {
return $this->maxWinningAmount;
}
// 生成开奖号码
public function generateWinningNumber() {
$redBalls = [];
// 随机生成6个不重复的红球号码(1-33)
while (count($redBalls) < 6) {
$ball = mt_rand(1, 33);
if (!in_array($ball, $redBalls)) {
$redBalls[] = $ball;
}
}
sort($redBalls); // 对红球号码进行排序
$blueBall = mt_rand(1, 16); // 随机生成一个蓝球号码(1-16)
return array(
'redBalls' => $redBalls,
'blueBall' => $blueBall
);
}
}
// 示例用法
$lottery = new DoubleColorBall();
$numberOfTickets = 100000; // 指定购买的号码数量
$pricePerTicket = 2; // 每张彩票的价格
$selectedNumbers = $lottery->generateRandomNumbers($numberOfTickets);
$winningNumber = $lottery->generateWinningNumber();
$totalCost = $lottery->calculateCost($selectedNumbers, $pricePerTicket);
$totalWinnings = $lottery->calculateWinnings($selectedNumbers, $winningNumber);
$profit = $totalWinnings - $totalCost;
$maxWinningNumber = $lottery->getMaxWinningNumber();
$maxWinningAmount = $lottery->getMaxWinningAmount();
echo "开奖号码是:" . json_encode($winningNumber) . "\n";
echo "您购买了" . $numberOfTickets . "张彩票,总成本为:" . $totalCost . "元\n";
echo "中奖金额为:" . $totalWinnings . "元\n";
echo "利润为:" . $profit . "元\n";
echo "中奖最大的一注号码是:" . implode(",", $maxWinningNumber['redBalls']) . ",蓝球号码是:" . $maxWinningNumber['blueBall'] . "\n";
echo "中奖最大金额为:" . $maxWinningAmount . "元\n";
?>
这里我模拟购买了10万张彩票(真买的话要20万元,哈哈哈),下面是运行结果:

评论 (0)