Demo entry 3883283

simulation

   

Submitted by anonymous on Mar 05, 2016 at 05:59
Language: Python. Code size: 8.4 kB.

from random import random, randrange, uniform, randint, shuffle, sample
import copy

class Deck():
    def __init__(self, size):
        self.l = [i for i in range(1, size + 1)]
                
    def chooseK(self, k):
        return sample(self.l, k)
        
    def kAreRed(self, k):
        deal = self.chooseK(k)
        for card in deal:
            if Card(card).isRed() is False:
                return False          
        return True
        
    def removeRedKing(self):
        redKings = [12, 25]
        king = sample(redKings, 1)[0]       
        self.l.remove(king)  
        
    def removeBlackAce(self):
        blackAces = [39, 52]
        ace = sample(blackAces, 1)[0]       
        self.l.remove(ace)
                        

class Card():
    def setN(self, n):
        if n is not None:
            self.n = n
            
    def __init__(self, n=None):
        self.n  = n

    def isHeart(self, n=None):
        self.setN(n)
        return self.n in range(1, 14)
        
    def isDiamond(self, n=None):
        self.setN(n)
        return self.n in range(14, 27)    

    def isSpade(self, n=None):
        self.setN(n)
        return self.n in range(27, 40)
        
    def isClub(self, n=None):
        self.setN(n)
        return self.n in range(40, 53)
        
    def isRed(self, n=None):
        self.setN(n)
        return self.isHeart() or self.isDiamond()

    def isBlack(self, n=None):
        self.setN(n)
        return  self.isSpade() or self.isClub()
        
    def isRedKing(self):
        return self.n == 12 or self.n == 25

class MagicCard:
    def __init__(self, isLand=False):
        self.isLand = isLand
    
    def setLandCard(self):
        self.isLand = True
        
    def setOtherCard(self):
        self.isLand = False
        
    def isLand(self):
        return self.isLand

class MagicDeck:
    def __init__(self, land, other):
        self.land = land
        self.other = other
        self.cards = [MagicCard(True) for i in range(0, land)] + [MagicCard(False) for i in range(0, other)]
    
    def chooseKLandCards(self, k):        
        for i in self.cards:
            if k > 0:
                if i.isLand:
                    self.cards.remove(i)
                    k -= 1
            else:
                break
    
    def chooseK(self, k=7):            
        return sample(self.cards, k) 
        
    def getLandCount(self, cards):
        count = 0
        for i in cards:
            if cards.isLand():
                count += 1
        return count


# 5.13 
# (b) What is the probability all four are red?
#
def prob5_13_b(n):
    cards = Deck(52)
    total = 0
    isRed = 0
    for i in range(n):
        if cards.kAreRed(4):
            total+=1 
            isRed+=1
        else:
            total+=1
    return float(isRed)/float(total)    


# 5.13 
# (c) What is the probability each has a different suit?
#    
def prob5_13_c(n):
    cards = Deck(52)
    total = 0
    success = 0    
    for i in range(n):
        suits = [Card().isClub, Card().isDiamond, Card().isHeart, Card().isSpade]
        choose = cards.chooseK(4)
        for i in choose:
            for x in range(len(suits)):
                if suits[x](i) is True:
                    del suits[x]
                    break
        total += 1
        if len(suits) == 0:
            success += 1    
    return float(success)/float(total)
    
    
    
# 5.27
# (b) P1:10 land cards,  P2:20. With what probability will P1:2 Land and P2:3 Lands?    
# 
def prob5_27_b(n):
    total = 0
    success = 0
    
    player1 = MagicDeck(10,30)
    player2 = MagicDeck(20,20)        
        
    for i in range(n):
        p1Deal = player1.chooseK()
        p2Deal = player2.chooseK()
        p1LandCount = 0
        p2LandCount = 0
        
        for x in p1Deal:
            if x.isLand:
                p1LandCount += 1
        for x in p2Deal:
            if x.isLand:
                p2LandCount += 1
        total += 1
        if p1LandCount == 2 and p2LandCount == 3:
            success += 1
    return float(success)/float(total)
    
    
    
# 5.27
# (c) P1:10 land cards,  P2:20. With what probability will P1: Land < P2:Lands?    
# 
def prob5_27_c(n):
    total = 0
    success = 0
    
    player1 = MagicDeck(10,30)
    player2 = MagicDeck(20,20)        
        
    for i in range(n):
        p1Deal = player1.chooseK()
        p2Deal = player2.chooseK()
        p1LandCount = 0
        p2LandCount = 0
        
        for x in p1Deal:
            if x.isLand:
                p1LandCount += 1
        for x in p2Deal:
            if x.isLand:
                p2LandCount += 1
        total += 1
        # print ( "P1: " + str(p1LandCount) + "  P1: " + str(p1LandCount))
        if p1LandCount < p2LandCount:
            success += 1
    return float(success)/float(total)
    

    
    
# 5.32. You take a standard deck of cards, shuffle it, and remove one card. You then draw a card.
# (b) What is the conditional probability that the card you draw is a red king, conditioned on the removed card being a red king?
# 
def prob5_32_b(n):    
    total = 0
    success = 0
    for i in range(n):
        deck = Deck(52)
        deck.removeRedKing()
        card = Card(deck.chooseK(1)[0])
        if card.isRedKing():
            total+=1 
            success+=1
        else:
            total+=1
    return float(success)/float(total)    
    
    
    
# 5.32. You take a standard deck of cards, shuffle it, and remove one card. You then draw a card.
# (c) What is the conditional probability that the card you draw is a red king, conditioned on the removed card being a black ace?
# 
def prob5_32_c(n):    
    total = 0
    success = 0
    for i in range(n):
        deck = Deck(52)
        deck.removeBlackAce()
        card = Card(deck.chooseK(1)[0])
        if card.isRedKing():
            total+=1 
            success+=1
        else:
            total+=1
    return float(success)/float(total)    
    
    
    
# 5.37. Magic the Gathering is a popular card game. P1:10 land cards. A player draws seven cards but does not look at them. The player then chooses three of these cards at random; each of these three is a land.
# (a) What is the conditional probability that the original hand of seven cards is all lands?
#    
def prob5_37_a(n):
    total = 0
    success = 0
    
            
        
    for i in range(n):
        cards  = MagicDeck(10,30)
        cards.chooseKLandCards(3)
        deal = cards.chooseK(4)        
        landCount = 3                        
                
        for card in deal:
            if card.isLand:
                landCount += 1
            
        total += 1
        if landCount == 7:
            success += 1
    return float(success)/float(total)    
    
    
# 5.37. Magic the Gathering is a popular card game. P1:10 land cards. A player draws seven cards but does not look at them. The player then chooses three of these cards at random; each of these three is a land.
# (b) What is the conditional probability that the original hand of seven cards contains only three lands?    
# 
def prob5_37_b(n):
    total = 0
    success = 0
                      
    for i in range(n):
        cards  = MagicDeck(10,30)
        cards.chooseKLandCards(3)
        deal = cards.chooseK(4)        
        landCount = 3                        
                
        for card in deal:
            if card.isLand:
                landCount += 1
            
        total += 1
        if landCount == 3:
            success += 1
    return float(success)/float(total)        
    
    
    
    
# 5.13.b N=100,000 P=0.05425    
print("5.13.b : " + str(prob5_13_b(100000)))

# 5.13.c N=100,000 P=0.10549
print("5.13.c : " + str(prob5_13_c(100000)))

# 5.27.b N=100,000 P=0.10177
print("5.27.b : " + str(prob5_27_b(100000)))

# 5.27.c N=100,000 P=0.78721
print("5.27.c : " + str(prob5_27_c(100000)))

# 5.32.b N=100,000 P=0.01939
print("5.32.b : " + str(prob5_32_b(100000)))

# 5.32.c N=100,000 P=0.03868
print("5.32.c : " + str(prob5_32_c(100000)))

# 5.37.a N=100,000 P=0.00048
print("5.37.a : " + str(prob5_37_a(100000)))

# 5.37.b N=100,000 P=0.4161
print("5.37.b : " + str(prob5_37_b(100000)))

#  For N = 100,000 iterations
# 5.13.b : 0.05425
# 5.13.c : 0.10549
# 5.27.b : 0.10177
# 5.27.c : 0.78721
# 5.32.b : 0.01939
# 5.32.c : 0.03868
# 5.37.a : 0.00048
# 5.37.b : 0.4161

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).