I have this code which is basically flipping a coin and when it hits heads (1) it flips again until the probability of it keep flipping heads is lower than 0.1% or when it hits tails it start again.
import numpy
def checkAgain(probability):
if(probability >= 0.1):
runCode()
def flipCoin(successes):
rand = numpy.random.randint(2)
if (rand == 1):
# true
successes += 1
flipCoin(successes)
else:
probability = 50
for i in range(successes):
probability /= 2
print(str(successes) + " " + str(probability) + "%")
checkAgain(probability)
def runCode():
successes = 0
flipCoin(successes)
runCode()
But the code only works sometimes. Most of the time I get this error: maximum recursion depth exceeded in comparison
I read online that this prevents "a stack overflow" but I have no idea how I can make the code run untill the probability is lower than 0.1