MathProbabilityProbabilistic modeling

Discrete random variables

Theory

Coin toss simulation count

Report a typo

Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!

A Python function is needed to simulate the flipping of a coin 100 times, tracking the occurrence of heads. Each coin flip is independent and has an equal chance of resulting in heads or tails, represented as 1 for heads and 0 for tails, respectively.

Your task is to fill in the blanks in the provided code to complete the function `flip_coin(n)` which takes the number of coin flips as an argument and returns the count of how many times the coin landed on heads. The function utilizes the random module to generate the outcomes of each flip. After defining the function, utilize it to determine the number of heads in 100 coin flips, and output the result in a sentence format.

Fill in the gaps with the relevant elements
import 

def flip_coin(n):
    return sum(random.([0, 1]) for _ in range(n))

result = flip_coin(100)
output = f"Number of heads in 100 coin flips: {result}"
print(output)
___

Create a free account to access the full topic