Advertisement
Guest User

Untitled

a guest
Apr 11th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. import random
  2.  
  3. def newdeck(n):
  4.     assert n % 2 == 0
  5.     cards = (['red'] * (n/2)) + (['black'] * (n/2))
  6.     random.shuffle(cards)
  7.     return cards
  8.  
  9. def play():
  10.     deck = newdeck(52)
  11.     money = 0
  12.     count = 0
  13.     while deck:
  14.         bet = False
  15.         if count > 0:
  16.             bet = True
  17.  
  18.         card = deck.pop()
  19.         if bet:
  20.             if card == 'red':
  21.                 money += 100
  22.             else:
  23.                 money -= 100
  24.         if card == 'red':
  25.             count -= 1
  26.         else:
  27.             count += 1
  28.  
  29.         #print '%5s %5s %d' % (bet, card, money)
  30.        
  31.     return money
  32.  
  33. total = 0
  34. trials = 1000
  35. for i in range(trials):
  36.     total += play()
  37. print total / float(trials)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement