﻿ 使用Python计算玩彩票赢钱概率 - 军军小站|张军博客

# 使用Python计算玩彩票赢钱概率

1. 随机购买，人是感性动物，很容易受周围环境干扰。完全随机购买在生活中是不存在的。
2. 分析数字在前面N期出现的概率，选择概率最小的n个数字购买。

```
import numpy as np
```

```
def play_lottery(my_money,play_num,money):
'''

result：结果字典
num：用于统计每个数字出现次数
my_money：我的余额
play_num：投注期数
money：投注额
flag: 1代表赢钱 0则相反 2表示不输不赢

'''
flag = 1
temp = my_money
result = {'0':0,
'1':1,
'2':2,
'3':3,
'4':4,
'5':5,
'6':6,
'7':7,
'8':8,
'9':9}
data = np.random.randint(0,10,(play_num,1))
num = np.zeros((10,1))
for i in data:
play = np.random.randint(0,10,(5,1))
if i in play:
temp = temp + money
else:
temp = temp - money
if result[str(i[0])] == i[0]:
num[i[0]] = num[i[0]] + 1
print('我的余额：',temp)
print('开奖结果统计：\n',num[0][0],num[1][0],num[2][0],num[3][0],num[4][0],num[5][0],num[6][0],num[7][0],num[8][0],num[9][0])
if temp < my_money:
flag = 0
elif temp == my_money:
flag = 2
return flag
```

```
i = 0
binary = np.zeros((3,1))
while i < 10000:
binary[play_lottery(1000,10,10)][0] += 1
i = i + 1
```

```
def play_lottery2(my_money,play_num,money):
'''

result：结果字典
num：用于统计每个数字出现次数
my_money：我的余额
play_num：投注期数
money：投注额
flag: 1代表赢钱 0则相反 2表示不输不赢
'''
flag = 1
temp = my_money
result = {'0':0,
'1':1,
'2':2,
'3':3,
'4':4,
'5':5,
'6':6,
'7':7,
'8':8,
'9':9}
data = np.random.randint(0,10,(play_num,1))
num = np.zeros((10,1))
num_first = np.random.randint(0,10,(10,1))
is_one = 1
for i in data:
if is_one == 1:
play = np.argsort(num_first,axis=0).reshape(-1,1)[0:5]
is_one = 0
play = np.argsort(num,axis=0).reshape(-1,1)[0:5]
if i in play:
temp = temp + money
else:
temp = temp - money
if result[str(i[0])] == i[0]:
num[i[0]] = num[i[0]] + 1
print('我的余额：',temp)
print('开奖结果统计：\n',num[0][0],num[1][0],num[2][0],num[3][0],num[4][0],num[5][0],num[6][0],num[7][0],num[8][0],num[9][0])
if temp < my_money:
flag = 0
elif temp == my_money:
flag = 2
return flag
```

```
i = 0
binary = np.zeros((3,1))
while i < 10000:
binary[play_lottery2(1000,10,10)][0] += 1
i = i + 1
```

QQ号联系： 360901061

【本文对您有帮助就好】