Java手动实现一个双色球游戏,每天自己买个彩票玩

前言

之前购买彩票时很少中奖,实际上彩票的中奖概率非常低。在多次尝试后,只中过一次。考虑到中奖机会有限,我决定尝试实现一个彩票自动购买程序,类似于机选功能。这样,我可以更加灵活地进行尝试,而不受实际彩票中奖概率的限制。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package demo.game;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;

import java.util.*;

/**
* @author jisl on 2023/7/31 15:52
* 双色球彩票
*/

public class DoubleColorBallLottery {

// 彩票期数
private int lotteryNumber = 0;

// 开奖号码
private Set<Integer> winRedNum = new HashSet<>();
private int winBlueNum;

// 用户购买的彩票
private final List<List<Integer>> userLottery = new ArrayList<>();

// 用户中奖的彩票
private final Map<Integer, List<String>> userPrizeLottery = new HashMap<>();

public int[] lotteryAmount = new int[]{0, 5000000, 1250000, 3000, 200, 10, 5};

private static final int MAX_PRIZE_LEVEL = 6;

Random random = new Random();

/**
* 彩票开奖
*
* @author jisl on 2023/7/31 16:06
**/
public void lotteryDrawing() {
winRedNum = buildRedNum();
winBlueNum = buildBlueNum();
System.out.printf("[第%s期双色球]开奖结果:红球:%s,蓝球:%s%n", lotteryNumber, JSON.toJSONString(winRedNum), winBlueNum);
}

private int buildBlueNum() {
return random.nextInt(16) + 1;
}

private HashSet<Integer> buildRedNum() {
final HashSet<Integer> set = new HashSet<>();
while (set.size() < 6) {
final int randomNum = random.nextInt(33) + 1;
set.add(randomNum);
}
return set;
}

/**
* 买彩票
*
* @author jisl on 2023/7/31 16:07
**/
public void buy(int count) {
lotteryNumber = RandomUtil.randomInt(999);
for (int i = 0; i < count; i++) {
final ArrayList<Integer> lottery = new ArrayList<>(buildRedNum());
lottery.add(buildBlueNum());
userLottery.add(lottery);
}
}

public void redemption() {
// 一到六等奖
for (int i = 1; i <= MAX_PRIZE_LEVEL; i++) {
userPrizeLottery.put(i, new ArrayList<>());
}
for (List<Integer> lottery : userLottery) {
int readBallMatches = countMatchingNumbers(lottery);
boolean blueBallMatch = lottery.get(6) == winBlueNum;
int prizeItem = calculatePrize(readBallMatches, blueBallMatch);
if (prizeItem != 0) {
// 中奖了
final List<String> numbers = userPrizeLottery.get(prizeItem);
numbers.add(JSON.toJSONString(lottery));
}
}
System.out.println(String.format("[第%s期双色球]中奖结果:", lotteryNumber));
int userPrizeCount = 0;
int userPrizeAmount = 0;
for (int i = 1; i <= MAX_PRIZE_LEVEL; i++) {
final int userPrizeItemCount = userPrizeLottery.get(i).size();
userPrizeCount += userPrizeItemCount;
userPrizeAmount += lotteryAmount[i] * userPrizeItemCount;
if (i <= 3) {
System.out.println(String.format("%s等奖中奖个数:%s,中奖号码:%s", i, userPrizeItemCount, userPrizeLottery.get(i)));
} else {
System.out.println(String.format("%s等奖中奖个数:%s,中奖号码:-", i, userPrizeItemCount));
}
}
// 共计中奖率:6.71%
System.out.println(String.format("一共买了[%s张]彩票,中奖个数=%s,中奖概率=%s;购买彩票费用=%s,奖金=%s,盈利=%s"
, userLottery.size(), userPrizeCount, 1.0 * userPrizeCount / userLottery.size(), userLottery.size() * 2, userPrizeAmount, userPrizeAmount - userLottery.size() * 2));
}


private int calculatePrize(int readBallMatches, boolean blueBallMatch) {
if (readBallMatches == 6 && blueBallMatch) {
return 1;
} else if (readBallMatches == 6) {
return 2;
} else if (readBallMatches == 5 && blueBallMatch) {
return 3;
} else if (readBallMatches == 5 || (readBallMatches == 4 && blueBallMatch)) {
return 4;
} else if (readBallMatches == 4 || (readBallMatches == 3 && blueBallMatch)) {
return 5;
} else if (blueBallMatch) {
return 6;
}
return 0;
}

private int countMatchingNumbers(List<Integer> lottery) {
int count = 0;
for (int i = 0; i < 6; i++) {
if (winRedNum.contains(lottery.get(i))) {
count += 1;
}
}
return count;
}

public void game(int n) {
buy(n);
lotteryDrawing();
redemption();
}

public static void main(String[] args) {
final DoubleColorBallLottery lottery = new DoubleColorBallLottery();
lottery.game(100000);
}


}

执行效果

1
2
3
4
5
6
7
8
9
10
[第88期双色球]开奖结果:红球:[2,3,19,23,13,29],蓝球:15
[第88期双色球]中奖结果:
1等奖中奖个数:0,中奖号码:[]
2等奖中奖个数:0,中奖号码:[]
3等奖中奖个数:0,中奖号码:[]
4等奖中奖个数:43,中奖号码:-
5等奖中奖个数:752,中奖号码:-
6等奖中奖个数:5957,中奖号码:-
一共买了[100000张]彩票,中奖个数=6752,中奖概率=0.06752;购买彩票费用=200000,奖金=45905,盈利=-154095

概率计算

一等奖(6+1)中奖概率为:

红球33选6乘以蓝球16选1===

二等奖(6+0)中奖概率为:

红球33选6乘以蓝球16选15(红色都中了,蓝色没中是)=

三等奖(5+1)中奖概率为:

红球33选5乘以蓝球16选1(红色剩下27个数字可以选没中,任意一个球)==

四等奖(5+0、4+1)中奖概率为:

红球33选5乘以蓝球16选15+红球33选4乘以蓝球16选1=$27615+65/227*26/2$=7695/17721088 (4+1,从27个数可以随机选出两个数,选出的两个数和顺序无关)

五等奖(4+0、3+1)中奖概率为:

红球33选4乘以蓝球16选15+红球33选3乘以蓝球16选1=2726/265/215+654/62726*25/6=137475/17721088

六等奖(2+1、1+1、0+1)中奖概率为:

红球33选0乘以蓝球16选1= 6543/(432)27262524/(432)+65432/(5432)2726252423/(5432)+654321/(65432)272625242322/(65432)=1043640/17721088
买了所有的彩票17721088组合,花费17721088
2=35442176=3500万,一共中奖金额=10436405+13747510+7695200+1623000+155106+5*106=88617950。共计中奖率:6.71%

Java手动实现一个双色球游戏,每天自己买个彩票玩

http://example.com/2023/11/17/Java手动实现一个双色球游戏,每天自己买个彩票玩/

作者

纪东东

发布于

2023-11-17

更新于

2023-11-17

许可协议