Created
July 8, 2024 12:33
-
-
Save BytefishMedium/0f06ee247c6fef7007d027e19e5bc913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def simulate_game(): | |
"""Simulate a single game where A and B throw the die alternately.""" | |
while True: | |
# Player A's turn | |
if random.randint(1, 6) == 6: | |
return "A" | |
# Player B's turn | |
if random.randint(1, 6) == 6: | |
return "B" | |
def estimate_probability(num_trials): | |
"""Estimate the probability of the first player (A) winning.""" | |
a_wins = 0 | |
for _ in range(num_trials): | |
if simulate_game() == "A": | |
a_wins += 1 | |
return a_wins / num_trials | |
# Number of trials to run the simulation | |
num_trials = 1000000 # 1 million trials for better accuracy | |
# Estimate the probability | |
probability_A_wins = estimate_probability(num_trials) | |
print(f"Estimated probability that A wins: {probability_A_wins:.5f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment