-
Notifications
You must be signed in to change notification settings - Fork 0
/
随机概率设置.py
70 lines (53 loc) · 1.71 KB
/
随机概率设置.py
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
# coding: utf-8
# In[51]:
import random
def random_index(rate):
"""随机变量的概率函数"""
#
# 参数rate为list<int>
#
start = 0
randnum = random.randint(1, sum(rate))
print('randnum=' + str(randnum))
for index, item in enumerate(rate):
print(str(index) + '-----' + str(item))
start += item
print('start=' + str(start))
print('randnum=' + str(randnum) + '??' + 'start=' + str(start))
if randnum <= start:
print('index=' + str(index))
print('---------------------------------------------------------------------------')
break
return index
def main():
arr = ['red', 'green', 'blue']
rate = [45, 30, 25]
red_times = 0
green_times = 0
blue_times = 0
i = 0
for i in range(10000):
index = random_index(rate)
if arr[index] == 'red':
red_times += 1
print (red_times, green_times, blue_times)
print('==================================================================================')
continue
if arr[index] == 'green':
green_times += 1
print (red_times, green_times, blue_times)
print('==================================================================================')
continue
if arr[index] == 'blue':
blue_times += 1
print (red_times, green_times, blue_times)
print('==================================================================================')
continue
print(i)
print (red_times, green_times, blue_times)
print (red_times+ green_times+ blue_times)
# In[52]:
main()
# In[53]:
main()
# In[ ]: