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; prints number of winners M = 0 for x in range(0,100): R = random.random() if R < 18./38: M = M + 1 print ('number of players who won: ', M) def roulette(): #one player starts with $500 and bets $10 100 times M = 500 #starting bankroll L=[M] for x in range(0,100): R = random.random() if R < 18./38: M = M + 10 else: M = M - 10 L.append(M) print ('money retained by player: ', M) pyplot.plot(L) pyplot.title('Money left to player vs. number of rolls') pyplot.xlabel('Number of rolls') pyplot.ylabel('Money left') pyplot.show() def roulettehundred(): # 100 players bet $10 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,1001,10):# the lists are formed L1.append(n-500) L2.append(0) for x in range(0,100):#this loop simulates 100 playeres M = 500 #initial money for each player for x in range(0,100):#this loop simulates 100 bets R = random.random() if R < 18./38: M = M + 10 else: M = M - 10 L2[int(M/10)]=L2[int(M/10)]+1 print (M - 500, end=' ') #winnings or losses of player win = win + (500-M) print ('') print ('casino wins', win) pyplot.bar(L1,L2,10) pyplot.title('Frequencies of gains or losses for players') pyplot.xlabel('Gains or losses') pyplot.ylabel('Frequency') pyplot.show() def casino(): #one player bets $10 at a time until money is doubled or gone M = 500 L=[M] while M < 1000 and M > 0: R = random.random() if R < 18./38: M = M + 10 print (M) if M == 1000: print ("") print ("you doubled your money") else: M = M - 10 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 rolls') pyplot.xlabel('Number of rolls') pyplot.ylabel('Money left') pyplot.show() def bigcasino(): #100 players bet $10 at a time until money is doubled or gone L = 0 for n in range(0,100): M = 500 while M < 1000 and M > 0: R = random.random() if R < 18./38: M = M + 10 if M == 1000: print ("") print ("you doubled your money") else: M = M - 10 if M == 0: print ("casino wins, ", end='') L = L + 1 print ("") print ("") print (L, "people lost all their money") if 100 - L == 1: print (100-L,'person doubled his money') else: print (100-L, 'people doubled their money') def roulettehundred2(): #1000 players bet 100 times, display money left, bar graph wins 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,1001,10): L2.append(0) L3.append(n-500) for x in range(0,1000): M = 500 for x in range(0,100): R = random.random() if R < 18./38: M = M + 10 else: M = M - 10 L1.append(M) L2[int(M/10)]=L2[int(M/10)]+1 L1.sort() print (L1) print (L2) for m in range(0,50): print (2*m, L2[2*m]) pyplot.bar(L3,L2,10) pyplot.title('Wins or losses of 1000 players after 100 rolls') pyplot.ylabel('Frequency') pyplot.xlabel('Wins or losses') pyplot.show()