Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My unfair coin flipping code is giving conflicting results with theory

I was reading the wikipedia page for the Gambler’s ruin concept, and I decided to quickly code it myself as a quick challenge. Just like the example in the "Unfair coin flipping" part on the right, I created an unfair coin with a 0.6 probability for heads and 0.4 probability for tails. Player 1 wins if heads, player 2 wins if tails. Finally, player 1 starts with 5 pennies and player 2 with 10 pennies. If one player wins, they win a penny and the other player loses one penny.

Here’s my code:

import random as rd

def unfair_flip(sc1, sc2):  # function takes scores of player 1 and player 2
    coin = ['heads'] * 6 + ['tails'] * 4  # P = 0.6 for player 1
    flip1, flip2 = rd.choice(coin), rd.choice(coin)
    if flip1 == 'heads':  # if player 1 wins...
        sc1 += 1
        sc2 -= 1
    if flip2 == 'tails':  # if player 2 wins...
        sc1 -= 1
        sc2 += 1
    return sc1, sc2


def proba_check(it):  # function takes the nb of iterations
    wins_p1 = 0
    for _ in range(it):
        player1, player2 = 5, 10  # player 1 starts with 5 pennies, player 2 with 10
        while 0 < player1 < 15:
            player1, player2 = unfair_flip(player1, player2)
        if player1 == 15:
            wins_p1 += 1
    return round(wins_p1/it, 3)


print(f"Probability of winning for Player 1: {proba_check(10000)}")

Here’s the weird thing: using the formula from the wiki page, the win probability for player 1 should be ~0.8703. But when I run my program with a decent number of iterations, say 10 000, I usually get a win probability of around 0.98 for player 1. I’ve reviewed my code several times and I can’t find where I’m wrong. I’ve been coding from time to time for the last 4 years so I consider this an easy challenge haha, but somehow I can’t get it to work… Also, is there a quicker way to code an unfair coin flip than how I did it?
Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The example of the "Unfair coin flipping" is based on flipping one coin. In this code you are flipping two coins and checking their value independently, which changes the probabilities.

To get the expected result of ~0.8703, you should flip one coin and attribute the results of the penny exchange accordingly, like so:

def unfair_flip(sc1, sc2):  # function takes scores of player 1 and player 2
    coin = ['heads'] * 6 + ['tails'] * 4  # P = 0.6 for player 1
    flip = rd.choice(coin) # 1 flip for both players, instead of 1 flip for each
    if flip == 'heads':  # if player 1 wins...
        sc1 += 1
        sc2 -= 1
    else:  # if player 2 wins...
        sc1 -= 1
        sc2 += 1
    return sc1, sc2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading