forked from pdsteele/DES-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhat.py
More file actions
85 lines (67 loc) · 3.02 KB
/
hat.py
File metadata and controls
85 lines (67 loc) · 3.02 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
75
76
77
78
79
80
81
82
83
84
85
# # ---------------------------------------------------------------------- *
# * A Monte Carlo simulation of the hat check girl problem. *
# * *
# * Name : hat.c *
# * Authors : Steve Park & Dave Geyer *
# * Language : ANSI C *
# * Latest Revision : 09-16-95 *
# # Translated by : Philip Steele
# # Language : Python 3.3
# # Latest Revision : 3/26/14
# * ---------------------------------------------------------------------- */
from rng import putSeed, random
# global variables */
#i # replication index */
#arr # array */
count = 0 # # of times a match occurs */
#p # probability estimate */
SIZE = 10 # array size */
N = 10000 # number of replications */
def Equilikely(a,b):
# # ------------------------------------------------
# * generate an Equilikely random variate, use a < b
# * ------------------------------------------------
# */
return (a + int((b - a + 1) * random()))
# ============================== */
def Initialize(a):
# ============================== */
for j in range(0,SIZE):
a[j] = j
# =========================== */
def Shuffle(a):
# =========================== */
for j in range(0,SIZE-1): # shuffle an array */
t = Equilikely(j, (SIZE - 1)) # in such a way that all */
hold = a[j] # permutations are equally */
a[j] = a[t] # likely to occur */
a[t] = hold
# ============================ */
def Check(a):
# ============================ */
j = 0
test = 0
condition = True
while(condition==True): # test to see if at least */
test = (a[j] == j) # one element is in its */
j += 1 # 'natural' position */
condition = (j != SIZE) and (test==0) # - return a 1 if so */
# - return a 0 otherwise */
if (test == 1):
return(1)
else:
return(0)
###############################Main Program##############################
putSeed(0)
arr = [None for i in range(0,SIZE)]
Initialize(arr)
for i in range(0,N): # do N Monte Carlo replications */
Shuffle(arr)
count += Check(arr)
p = float(N - count) / N # estimate the probability */
print("\nfor {0:1d} replications and an array of size {1:d}".format(N, SIZE))
print("the estimated probability is {0:5.3f}".format(p))
# c output:
# Enter a positive integer seed (9 digits or less) >> 123456789
# for 10000 replications and an array of size 10
# the estimated probability is 0.369