Skip to content
Extraits de code Groupes Projets
martingale.py 1,48 ko
Newer Older
  • Learn to ignore specific revisions
  • from random import random
     
     
    class game:
        def __init__(self, initial_stack = 1000):
            self.initial_stack = initial_stack
            self.money = initial_stack
            self.toBet = 1
            self.finished = False
            
        def play(self):
            win = random() < 18/37
            if win:
                self.money += self.toBet
                if self.money >= self.initial_stack + 50:
                    self.finished = True
            else:
                self.money -= self.toBet
                self.toBet *=2
                if self.toBet > self.money:
                    self.finished = True
    
    def get_prob_winning(initial_stack, ntries = 100000):
        nWin  = 0
        money = 0
    
        for i in range(ntries):
            g = game(initial_stack)
            while not g.finished:
                g.play()
            nWin  += (g.money >= initial_stack+50)
            money += g.money - initial_stack
        
        return (nWin/ntries,money/ntries)
    
    
    ww = get_prob_winning(1000)
    print("If you play with a 1000€ stack, you loose in {}% of the cases, leading to a {}€ average loss".format(100*(1-ww[0]),-ww[1]))
    
    
    stack     = 0
    threshold = .99
    
    for i in reversed(range(0,16)):
        stack += 1<<i
        prob = get_prob_winning(stack)[0]
        print("Stack = {} ---> prob = {} ".format(stack,prob))
        if prob >= threshold:
            stack -= 1<<i
            
    print("Stack to have {}% chance of winning has to be {}... Suspiciouly close to 32768...".format(100*threshold,stack))
    print("You will therefore loose on average {} euros".format(-get_prob_winning(stack)[1]))