Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168740
Image: ubuntu2004
###################################################################################### # Theory # # # P(1) = 4/52 # P(2) = (48/52) * (4/51) # P(3) = (48/52) * (47/51) * (4/50) # P(4) = (48/52) * (47/51) * (46/50) * (4/49) # ...... # P(k) = (48/52) * (47/51) *...* [(48-k+2)/(52-k+2)] * [4/(52-k+1)]
(k>=2) # ...... # P(49) = (48/52) * (47/51) *...* (1/5) * (4/4) # # # Average = P(1)*1 + P(2)*2 + P(3)*3 +...+ P(48)*48 + P(49)*49 # # As a result, the most frequently output will be 1 # the least frequently output will be 49 # the average output will be "Average". # ###################################################################################### def compute_p(k): result = 1 if k == 1: return 4/52 elif k == 0: return 0 else: for i in range(k-1): result = result * ((48-i)/(52-i)) result = result * (4/(52-k+1)) return result P_list = [] avg = 0 for i in range(50): p = compute_p(i) P_list.append(p) for i in range(len(P_list)): avg = avg + i*(P_list[i]) print (float(avg)) # The average /// 10.6
#
###################################################################################### # Experiment ###################################################################################### def create_deck(): """Create a deck of cards ranks is a list of the ranks suits is a list of the suits""" ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] suits = ['C', 'D', 'H', 'S'] deck = [] for suit in suits: for rank in ranks: card = (rank, suit) deck.append(card) return deck def shuffle_deck(deck): """Shuffle the deck by swapping pairs of cards""" for i in range(51): # i ranges from 0 to 50 j = randint(i+1,51) # j ranges from i+1 to 51 deck[i], deck[j] = deck[j], deck[i] def FirstAce(deck): for i in range(len(deck)): if deck[i][0] == 'A': return i+1 def create_list(): l = [] for i in range(50): l.append(0) return l deck = create_deck() l = create_list() avg = 0.0 for i in xrange(10000): shuffle_deck(deck) c = FirstAce(deck) l[c] = l[c] + 1 for i in range(1,len(l)): print i,":",l[i] avg = avg + l[i]*i avg = avg / 10000 print "\nAverage: ",avg
1 : 761 2 : 702 3 : 650 4 : 644 5 : 574 6 : 591 7 : 518 8 : 538 9 : 461 10 : 421 11 : 362 12 : 368 13 : 346 14 : 321 15 : 281 16 : 277 17 : 244 18 : 222 19 : 222 20 : 168 21 : 151 22 : 149 23 : 136 24 : 116 25 : 132 26 : 95 27 : 91 28 : 73 29 : 60 30 : 55 31 : 56 32 : 43 33 : 33 34 : 38 35 : 18 36 : 15 37 : 18 38 : 15 39 : 9 40 : 13 41 : 7 42 : 4 43 : 0 44 : 1 45 : 1 46 : 0 47 : 0 48 : 0 49 : 0 Average: 10.6491000000000