-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardSim.py
54 lines (46 loc) · 1.42 KB
/
cardSim.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
import random
def run_one(numCards):
cardArray = [-1] * numCards
count = 0
while not checkSet(cardArray):
cards = drawCards(numCards);
cardArray = addCards(cards, cardArray);
count = count + 1
return count
def drawCards(numCards):
packSize = 10
cardArray = [-1]*packSize;
i = 0;
count = 0
while i < packSize:
card = random.randint(1,numCards);
j = 0;
notFound = True;
while j < i and notFound:
if cardArray[j] == card:
i = i - 1;
notFound = False;
j = j + 1
if notFound:
cardArray[i] = card;
i = i + 1
return cardArray
def checkSet(cardArray):
for i in range(0, len(cardArray)):
if cardArray[i] != i + 1:
return False;
return True;
def addCards(newCards, cardArray):
for i in range(0, len(newCards)):
cardArray[newCards[i] - 1] = newCards[i];
return cardArray
def run_sim(itCount, numCards):
totalCount = 0
for i in range(0,itCount):
count = run_one(numCards);
totalCount = totalCount + count;
return float(totalCount) / itCount
print "Average Number of 10 card packs needed to get 100 Cards: ", run_sim(10000, 100);
print ""
#Can take a long time to run. Reduce first input to reduce iterations and run time
print "Average Number of 10 card packs needed to get 300 Cards: ", run_sim(10000, 300);