0

I am familiarizing my self with Monte Carlo simulations and have played around with the following code. It is a gambling dice rolling game with an increased payout for doubles. It uses RNG and pyplot. The for loop, print statements, and basic math operations are all working as desired (I think), but the graph gets constructed and no information is added. I have some coding experience through school (thus I got this far) but am new to python so would appreciate any help!

I am hoping to make 1 "live updating graph" that looks like this. monte carlo graph

Code shown below:

#dice game: rolling 2 die 100 times, hoping for doubles which give 4x payout
#import time

#required packages:
#for graphing
import matplotlib.pyplot as plt
#for random number generation
import random

#die rolling function
def roll_dice():
  die_1 = random.randint(1,6)
  die_2 = random.randint(1,6)
  #same number check
  if die_1 == die_2:
    same_num = True
    print(f"dice are same value of {die_1}")
  else:
    same_num = False
    print(f"dice are not the same. die 1 is {die_1} and die 2 is {die_2}")
  return same_num
#roll_dice()

#to repeat the rolls of each die
#need the following inputs
num_simulations = 100
max_num_rolls = 10

#tracking?
win_probability = []
end_balance = []

#looping simulation
for i in range(num_simulations):
  #setting inital values for each  variable
  balance = 50
  num_rolls = 0
  num_wins = 0
  bet = 1.00
  payout_factor = 4
  #run until hitting 1000
  for num_rolls in range(max_num_rolls):
    if roll_dice():
      balance=balance+payout_factor*bet
      num_wins += 1
    else:
      balance=balance-bet
      
  print(f"in {max_num_rolls} rolls, you won {num_wins} times")
  print(f"the balance after simulation {i} of {max_num_rolls} rolls was {balance}") 
  print(f"end of simulation {i}")
  plt.plot(max_num_rolls, balance)
  plt.xlabel('number of 2 dice rolls')
  plt.ylabel('balance $')
  legend_entries = []
  legend_entries.append('simulation '+str(num_simulations))
  
plt.show()
  
kca062
  • 53
  • 5
  • Welcome to [so]. Please read [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask). "please help me" is [not a question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question); we don't write code to specification. [You are expected to study the problem](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) ahead of time; think of logical steps to solve the problem; figure out exactly where you are stuck, and be able to show exact input and output. – itprorh66 May 29 '23 at 14:49
  • The line I think I am stuck on is '''plt.plot(max_num_rolls, balance)'''. The code works in that it runs through all the simulations. I think I have the indentation where the plotting should fall within the for loop. My plot result has axes and titles but no data. Hope that is more along the lines of what is a better question – kca062 May 30 '23 at 00:20

0 Answers0