Python using too much RAM to fully run

**Question: Am new to python language but have some coding background from recently finishing undergrad (understand basic logic, have learned some python syntax, etc.). I am trying to run a Monte Carlo Simulation that I found online as a learning exercise. Replit is handy as using so I don’t have to go through my IT department to request permission to install an actual application to run python (which of course is a slow and lovely process). Currently it works for the first run in the loop and then exceeds maximum allowable RAM usage. Hopefully will be doing between 1000-10,000 loops but I understand this could just be a limitation of the web browser version. If there are any suggestions to increase code efficiency that would be appreciated. **

Repl link: - Monte Carlo Simulation - Replit

#snippet of the for loop that I think is the problem: 
#looping simulation
for i in range(num_simulations):
  #setting inital values for each  variable
  balance = [50]
  num_rolls = [0]
  num_wins = 0
  #run until hitting 1000
  while num_rolls[-1] < max_num_rolls:
    #this same is not the boolean, calls the 
    same = roll_dice
    #code to execute when dice are same
    if same:
      num_wins += 1
    #code to execute when dice are different

code snippet

i believe if you make it pause for a couple milliseconds each iteration it will reduce ram usage to make it sleep you import the time library with import time then execute time.sleep(0.1) to make it sleep for 100 milliseconds here is a simple stopwatch that impliments this

import time
clock = 0
while True:
	time.sleep(0.1)#sleep for 100 millisecond
	clock += 1
	print(f"It has been {clock} second(s)")

It is 1000 miliseconds, not 100.

Ah that’s my bad sorry

Thank you for your replies! Unfortunately the RAM usage still reaches critical levels. Perhaps I am implementing it wrong as I am fairly new? I have the import statement outside the loop and the while segment as follows:

Should this line be actually inside the while loop?


A few RAM gobbling things in your repl:

  • import matplotlib.pyplot as plt. Matplotlib in my experience is not very efficient (And gobbles RAM)
  • Poetry (try setting guessImports = false in .replit)

You could probably use the debugger to find the point that produces the most RAM usage.


I believe the problem is that there is an infinite loop as the assignment to break it is outside of the loop.
See my previous post.


yes that did fix the RAM issue, thanks! But when running it is currently stuck on the first iteration still

You have a while True and I cannot understand how you get out of the loop …
Btw you can delete the sleep statements as with such code the only reason for ram usage is an error in the loops, often an infinite loop

1 Like

Yes i did remove the sleep statements. I think I understand what you mean… I thought I had set the initial number of rolls completed as zero and going through the sequence increases it by 1. this will stop when it hits the max which currently I have set as 3 but want to make into a much larger number, like 100 or 1000

You should use num_rolls = 0 and then just increase it num_rolls +=1 and check with while num_rolls < ...
I cannot understand why you use a list for doing this (huge memory waste btw).

The while True is also wrong as it will continue forever. But maybe this is what you want


Ok thank you. I will try and adjust and let you know how it goes. This is not 100% original code and I am still pretty new to python :slight_smile:

This is why the forum exists. Just ask and lot of people will try to help!


Some progress but still need to workout some more logic. I replaced the while with a for loop and it now goes through the correct number of times but it seems the dice rolling function doesnt get included?

Is roll_dice a function ? If so, you need roll_dice() to call it you you are just copying the code into same …

1 Like

Ah yes forgot about that syntax! The loop is now working (kinda) but my math operations are wrong and i know I have to change the if condition to somehow be same_num ouput instead of same :sweat_smile:.


Great! Feel free to ask if you get stuck again.
Please mark a post in here as a solution so that the thread can be closed.


Hope yall had a good long weekend! Am making some more adjustments and cant tell if its an indentation issue or if its how functions are called in python.

If roll_dice returns a boolean value under the name same_num, how do I call that value in a separate for loop? I thought it was roll_dice.same_num? If not should I somehow move the function into the for loop?



Try this:

if roll_dice():

Since your function returns a boolean, you don’t have to specify == True, and you are calling it incorrectly as roll_dice instead of roll_dice()