import random from matplotlib import pyplot def oneplay(): #one spin of the roulette R = random.random() if R < 18./38: print "player wins" else: print "casino wins" def oneplayhundred(): #one spin of the roulette by 100 people M = 0 for x in range(0,100): R = random.random() if R < 18./38: M = M + 1 print M def roulette(): #one player bets 100 times M = 50 L=[M] for x in range(0,100): R = random.random() if R < 18./38: M = M + 1 else: M = M - 1 L.append(M) print M pyplot.plot(L) pyplot.show() def roulettehundred(): # 100 players bet 100 times win = 0 # win will be the final win for the casino L1=[] # the range of wins or losses for the casino L2=[] # keeps track how many times each result M happened for n in range(0,100): L1.append(n-50) L2.append(0) for x in range(0,100): M = 0 for x in range(0,100): R = random.random() if R < 18./38: M = M + 1 else: M = M - 1 L2[M+50]=L2[M+50]+1 print M, win = win - M print "" print win pyplot.bar(L1,L2) pyplot.show() def casino(): #one player bets $1 at a time until money is doubled or gone M = 50 L=[M] while M < 100 and M > 0: R = random.random() if R < 18./38: M = M + 1 print M, if M == 100: print "" print "you doubled your money" else: M = M - 1 print M, if M == 0: print "" print "casino wins; you lost all your money" L.append(M) pyplot.plot(L) pyplot.title('Money left to player vs. number of roles') pyplot.xlabel('Number of roles') pyplot.ylabel('Money left') pyplot.show() def bigcasino(): #100 players bet $1 at a time until money is doubled or gone L = 0 for n in range(0,100): M = 50 while M < 100 and M > 0: R = random.random() if R < 18./38: M = M + 1 if M == 100: print "" print "you doubled your money" else: M = M - 1 if M == 0: print "casino wins, ", L = L + 1 print "" print "" print L, print "people lost all their money" print 100-L, if 100 - L == 1: print "person doubled his money" else: print "people doubled their money" def roulettehundred2(): #1000 players bet 100 times, display money left L1=[] #will keep track of amounts of money kept by players L2=[] #will count how many times each amount appeared L3=[] for n in range(0,100): L2.append(0) L3.append(n) for x in range(0,1000): M = 50 for x in range(0,100): R = random.random() if R < 18./38: M = M + 1 else: M = M - 1 L1.append(M) L2[M]=L2[M]+1 L1.sort() print L1 print L2 for m in range(0,50): print 2*m, L2[2*m] pyplot.bar(L3,L2) pyplot.title('Money retained by 1000 players after 100 roles') pyplot.ylabel('Frequency') pyplot.xlabel('Money retained') pyplot.show()