Avoiding exit code -1073741571 (0xc00000fd) when I need to run a sim calling functions many times over

-3

so I just programmed in pycharm a sort of blackjack simulation for card counting. The goal is to run many many hands of blackjack using perfect basic strategy and card counting and seeing based off the users bankroll and bet spread what their risk of losing all their money is. The problem is, in order to do that I need to run through virtual hands of blackjack many times until they lose their bankroll or they profit by a set amount. Each iteration of them playing until either losing or winning is handled by a function with a for n in range NumberOfSims. When the player finally loses all it's money or profits x, it goes to a function simwin() or simlost() where the looping stops and the next value in the for n in range NumberOfSims resets the bankroll and loops the functions until again they get to simwin() or simlost(). I got the maximum recursion error and used set recursion limit higher. Then I got the error: python process finished with exit code -1073741571 (0xc00000fd). I found this Process finished with exit code -1073741571 which mentions changing the threadsize, but I'm still a bit new to this and don't understand if it applies to my situation and how to use it.

Here is a basic version of my code.

import random
import math
import sys
deck = z = [2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
            10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
numberOfSims = 5 # This in the real code is user input
profit = 100 # User input
sys.setrecursionlimit(20000)
bet = 10
bankroll = 100 # User Input
ogbankroll = 100 # User input
simlosses = 0
simwins = 0
def start():
    for n in range(numberOfSims):
        playgame()
    print("Sim wins: ", simwins)
    print("Sim losses: ", simlosses)

def playgame():
    global deck
    global numberOfSims
    global profit
    global bet
    global bankroll

    random.shuffle(deck)
    random.shuffle(deck)
    random.shuffle(deck)
    if bankroll == ogbankroll + profit:
        simwin()
    elif bankroll <= 0:
        simlost()
    else:
        dealerhand = [deck[0], deck[1]]
        playerhand = [deck[2], deck[3]]
        if dealerhand[0] + dealerhand[1] == playerhand[0] + playerhand[1]:
            #Push
            playgame()
        elif dealerhand[0] + dealerhand[1] > playerhand[0] + playerhand[1]:
            # Lost
            bankroll = bankroll - bet
            playgame()
        else: # This else means player wins
            # dealerhand[0] + dealerhand[1] < playerhand[0] + playerhand[1]
            bankroll = bankroll + bet
            playgame()
def simwin():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    bankroll = ogbankroll
    simwins = simwins + 1

def simlost():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    bankroll = ogbankroll
    simlosses = simlosses + 1


start()

I left out functions that actually control the game like when the player hits when the dealer hits and ETC but this is basically the flow of the code. It has to go through hands many times because often the bankroll is something like 5000 and the player's max bet is like 20 so it takes a long time for the player to lose it all or profit.

python
asked on Stack Overflow Aug 16, 2020 by Mad Mellon • edited Aug 18, 2020 by Vadim Kotov

1 Answer

0

So I figured out how to do it with more iteration. I created a variable simIsDone, Then In the main function, playgame() at the end of defining all the functions such as hit() Stand() Etc, I put a while simIsDone == 0: playgame() then I removed all function calls to playgame() other than the for n in range. Then whenever you won with simwin() or lost with simlost() at the end I would turn simIsDone = 1 This would stop the while loop meaning that you either profited or lost all your bankroll. Then as a result the for n in range loop would check if we had completed all the simulations, if we still have more to do then we set simIsDone = 0 and then went to playgame() where the for loop would once again take over until a profit or loss happened. Below is the code fixed for this solution Note: For this basic minimal code it will still get the error because there is a 50 percent chance that any hand will lose or win so if you set the profit too high it will be mathematically improbable that you will ever win and thus the only way to end each iteration is if you lose all your money. So it's possible that you end up ahead a bunch but then since the set profit is so high you are likely to keep drawing hands where you keep winning and losing in a row thus never reacher bankroll == 0 or bankroll >= profit + ogbankroll

import random
import math
import sys
deck = z = [2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
            10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
numberOfSims = 5
# This in the real code is user input
profit = 5 # User input
sys.setrecursionlimit(20000)
bet = 1
bankroll = 10 # User Input
ogbankroll = 10 # User input
simlosses = 0
simwins = 0
simdone = 0
numberr = 0
def start():
    global simdone
    global numberr
    for n in range(numberOfSims):
        numberr = 0
        simdone = 0
        playgame()
    simdone = 1
    print("Sim wins: ", simwins)
    print("Sim losses: ", simlosses)

def playgame():
    global deck
    global numberOfSims
    global profit
    global bet
    global bankroll
    global numberr

    random.shuffle(deck)
    random.shuffle(deck)
    random.shuffle(deck)
    if bankroll == ogbankroll + profit:
        simwin()
    elif bankroll <= 0:
        simlost()
    else:
        dealerhand = [deck[0], deck[1]]
        playerhand = [deck[2], deck[3]]
        if dealerhand[0] + dealerhand[1] == playerhand[0] + playerhand[1]:
            ab = 1
            #Push

        elif dealerhand[0] + dealerhand[1] > playerhand[0] + playerhand[1]:
            # Lost
            bankroll = bankroll - bet

        else: # This else means player wins
            # dealerhand[0] + dealerhand[1] < playerhand[0] + playerhand[1]
            bankroll = bankroll + bet
    while simdone == 0:
        numberr = numberr + 1
        # print(numberr)
        playgame()

def simwin():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    global simdone
    bankroll = ogbankroll
    simwins = simwins + 1
    simdone = 1
def simlost():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    global simdone
    bankroll = ogbankroll
    simlosses = simlosses + 1
    simdone = 1


start()
answered on Stack Overflow Aug 17, 2020 by Mad Mellon

User contributions licensed under CC BY-SA 3.0