Skip to content

Quick simulation for the Monty Hall problem! take it with a grain of salt!

Notifications You must be signed in to change notification settings

zidokobik/Monty-Hall-Simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simulating Monty Hall's classic problem!

from game import Game
import random
def play_game(switch: bool = False) -> bool:
    """
    Play a game, randomly select a door, default to not switch door. Return Win-ness
    """
    game = Game()
    game.player_select(random.randint(0, 2))
    if switch:
        game.player_switch()

    if game.players_door.prized:
        return True
    return False
def simulate_samples(size: int, switch: bool = False):
    wins = 0
    for _ in range(size):
        if play_game(switch=switch):
            wins += 1
    
    print(f"After playing {size} games, Won {wins}. Probability: {wins/(size+1)}")

Simulate different ranges of sample sizes, never switch doors:

simulate_samples(1_000)
simulate_samples(10_000)
simulate_samples(100_000)
After playing 1000 games, Won 352. Probability: 0.3516483516483517
After playing 10000 games, Won 3382. Probability: 0.3381661833816618
After playing 100000 games, Won 33291. Probability: 0.3329066709332907

Simulate different ranges of sample sizes, always switch doors:

simulate_samples(1_000, switch=True)
simulate_samples(10_000, switch=True)
simulate_samples(100_000, switch=True)
After playing 1000 games, Won 640. Probability: 0.6393606393606394
After playing 10000 games, Won 6712. Probability: 0.6711328867113289
After playing 100000 games, Won 66636. Probability: 0.6663533364666353

Conclusion:

Always switch doors. From the simulation above, not switchind door yealds approximately 33% winrate while switching doors yealds approximately 66% winrate. vos Savant was right =))

About

Quick simulation for the Monty Hall problem! take it with a grain of salt!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published