forked from pdsteele/DES-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcraps.py
More file actions
74 lines (59 loc) · 2.76 KB
/
craps.py
File metadata and controls
74 lines (59 loc) · 2.76 KB
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
# ------------------------------------------------------------------------- *
# * A Monte Carlo simulation of the dice game Craps. *
# * *
# * Name : craps.c *
# * Author : Steve Park & Dave Geyer *
# * Language : ANSI C *
# * Latest Revision : 9-16-95 *
# * Compile with : gcc craps.c rng.c *
# Translated by : Philip Steele
# Language : Python 3.3
# Latest Revision : 3/26/14
# * ------------------------------------------------------------------------- */
from rng import putSeed, random
N = 10000 # number of replications */
# global variables */
# long i # replication index */
# long point # the initial roll */
# long result # 0 = lose, 1 = win */
wins = 0 # number of wins */
# double p # probability estimate */
# ============================== */
def Equilikely(a,b): # use a < b */
# ============================== */
return(a + int((b - a + 1) * random()))
# ============== */
def Roll():
# ============== */
return(Equilikely(1, 6) + Equilikely(1, 6))
# ==================== */ # roll until a 7 is obtained */
def Play(point): # - then return a 0 */
# ==================== */ # or the point is made */
# - then return a 1 */
condition = True
while(condition == True):
sum = Roll()
condition = (sum != point) and (sum != 7)
if(sum == point):
return(1)
else:
return(0)
############################Main Program#################################
putSeed(0)
for i in range(0,N): # do N Monte Carlo replications */
point = Roll()
if(point in [7,11]):
result = 1
elif(point in [2,3,12]):
result = 0
else:
result = Play(point)
wins += result
#EndFor
p = float(wins) / float(N) # estimate the probability */
print("\nfor {0:1d} replications".format(N))
print("the estimated probability of winning is {0:5.3f}\n".format(p))
# C output:
# Enter a positive integer seed (9 digits or less) >> 123456789
# for 10000 replications
# the estimated probability of winning is 0.485