Guest User

Untitled

a guest
Nov 3rd, 2013
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import random
  3. import itertools
  4.  
  5. # the set of sets of letters on each card
  6. s = [['A','B','C'], ['D','E','F','G'], ['H','I','J','K','L']]
  7.  
  8. # the deck of all possible cards
  9. deck = list(itertools.product(*s))
  10.  
  11. # the full sequence of letters in the entire deck
  12. allchars = set(list(itertools.chain.from_iterable(s)))
  13.  
  14.  
  15. # Monte Carlo iteration size
  16. samplingsize = 1000000
  17.  
  18. # iterate through multiple hand sizes
  19. print "sampling size = ", samplingsize
  20. for k in xrange(5, 41):
  21.  
  22. # the size of the hand
  23. handsize = k
  24.  
  25. # count the number of hands that were a success
  26. handsuccess = 0
  27.  
  28. for j in xrange(0, samplingsize):
  29.  
  30. thishand = []
  31. for i in xrange(0,handsize):
  32. # pick a card at random, insert it into hand
  33. thishand.append(deck[random.randint(0,len(deck) - 1)])
  34.  
  35. # get all unique chars in this hand
  36. thishandchars = set(list(itertools.chain.from_iterable(thishand)))
  37.  
  38. # compare the unique chars of the hand with the unique chars of the deck
  39. if(thishandchars == allchars):
  40. # hand contains all letters in deck
  41. handsuccess += 1;
  42.  
  43. print "For a hand size of {}, probability = {:.3f}%".format(handsize, 100.0 * handsuccess / samplingsize)
RAW Paste Data