Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update 8c-challenge-simulate-a-coin-toss-experiment.py#65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
SabineSkerries wants to merge1 commit intorealpython:master
base:master
Choose a base branch
Loading
fromSabineSkerries:patch-1
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,38 +12,29 @@
# 4. After the first toss, you'll need another loop to keep flipping while you
# get the same result as the first flip.

import random


def single_trial():
"""Simulate repeatedly a coing until both heads and tails are seen."""
# This function uses random.randint() to simulate a single coin toss.
# randing(0, 1) randomly returns 0 or 1 with equal probability. We can
# use 0 to represent heads and 1 to represent tails.

# Flip the coin the first time
flip_result = random.randint(0, 1)
# Keep a tally of how many times the coin has been flipped. We've only
# flipped once so the initial count is 1.
flip_count = 1

# Continue to flip the coin until randint(0, 1) returns something
# different than the original flip_result
while flip_result == random.randint(0, 1):
flip_count = flip_count + 1

# The last step in the loop flipped the coin but didn't update the tally,
# so we need to increase the flip_count by 1
flip_count = flip_count + 1
return flip_count
# Here is a simpler-to-read thus more pythonistic version, I think :P

import random

def flip_trial_avg(num_trials):
"""Calculate the average number of flips per trial over num_trials total trials."""
total = 0
for trial in range(num_trials):
total = total + single_trial()
return total / num_trials
def coin_flip_trial():
heads = 0
tails = 0
tries = 0
# As long as heads OR tails are zero, we have not had one of each.
while heads == 0 or tails == 0:
result = random.randint(0,1)
tries = tries + 1
if result == 1:
heads = heads + 1
else:
tails = tails + 1
return tries

# So let's now do that 10,000 times and then report the average number of trials it took.
tries_needed = 0
for x in range(10_000):
tries_needed = tries_needed + coin_flip_trial()

print(f"It takes on average {tries_needed / 10_000:.2f} tries to get both a heads and a tails.")


print(f"The average number of coin flips was {flip_trial_avg(10_000)}")

[8]ページ先頭

©2009-2025 Movatter.jp