I would like to execute a loop in a loop, but when I try to do that I catch a wrong calculation comparing to a singular loop.
My code with a singular loop that works:
betresult = []
#main parameters
budget = 1500
numofbets = 100
win = 7.5
loss = -15
for i in range(numofbets):
#random list: 1 - win, 0 - loss
test = np.random.choice(np.arange(0, 2), p=[0.333, 0.667])
betresult.append(test)
#number of win and loss
winbets = np.sum(betresult)
lossbets = numofbets - winbets
#sum of win and loss
winsum = winbets * win
losssum = lossbets * loss
#final sum
result = budget - (winsum + losssum)
print(result)
And I would like to put that code in a new loop, but if I make something obviously with while
or for
, I catch a wrong calculation.
With a singular loop I usually get one number between 1100 and 1800
.
Code with a wrong calculation:
betresult = []
#main parameters
budget = 1500
numofbets = 100
win = 7.5
loss = -15
for i in range(10):
for j in range(numofbets):
#random list: 1 - win, 0 - loss
test = np.random.choice(np.arange(0, 2), p=[0.333, 0.667])
betresult.append(test)
#number of win and loss
winbets = np.sum(betresult)
lossbets = numofbets - winbets
#sum of win and loss
winsum = winbets * win
losssum = lossbets * loss
#final sum
result = budget - (winsum + losssum)
print(result)
With a double loop I usually get a number under 0
. And the bigger a loop range, the bigger a number under 0.
An example of wrong execution (calculation):
1470.0
7.5
-1590.0
-3007.5
-4447.5
-5910.0
-7440.0
-8835.0
-10410.0
-11985.0