每次抽取后都重新洗牌。计算10000次随机抽取可得到同花的几率。我做的比较复杂,分别累计了四种花色分别出现了几次
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
|
import random list = [ "2" , "3" , "4" , '5' , '6' , '7' , '8' , '9' , '10' , "J" , "Q" , "K" , "A" ] list2 = [ "H" , "C" , "D" , "S" ] list3 = [] n = 0 a = 0 while a< 4 : n = 0 while n< 13 : list3 + = [ list [n] + list2[a]] n + = 1 a + = 1 i = 0 r = 0 d = 0 c = 0 s = 0 h = 0 while i < 10000 : random.shuffle(list3) list4 = list3[ 0 : 5 ] i + = 1 for card in list4: if 'D' in card: d + = 1 if d = = 5 : r + = 1 for card in list4: if 'H' in card: h + = 1 if h = = 5 : r + = 1 for card in list4: if 'S' in card: s + = 1 if s = = 5 : r + = 1 for card in list4: if 'C' in card: c + = 1 if c = = 5 : r + = 1 d = 0 c = 0 s = 0 h = 0 print ( 'Number of natural Flushes:' ,r) print ( 'Percentage:' ,r / 100 , '%' ) |
结果:
有关于运行代码保存路径的问题,如果是初学者的话,小编建议默认路径即可,我的是C:\python27,因为后来用到Django的时候吃过亏。。
总结
以上就是本文关于Python3简单实例计算同花的概率代码的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://www.open-open.com/code/view/1446432797701