Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 28
Image: ubuntu2004
Kernel: Python 3 (system-wide)

Suppose you flip 10 fair coins in a row. Estimate the probability that at least 5 of the coins will come up heads.

Assume 0 as tails and 1 as heads.

import numpy as np
def ten_fair(): #Simulate 10 flips of fair coin. flips = np.random.randint(0,2,size=10) #Generate a random integer from 0 to 1 with the size of 10, since we flip 10 times. heads = 0 #Counter for coin in flips: if coin == 1: heads += 1 #Count each time the coin gets heads. if heads >= 5: #Based on the result from the previous block, we return either 1 if we have at least 5 heads and 0 if we have less than 5 heads. return 1 else: return 0
ten_fair()
1

Then we will need to find the average of those tosses to determine the probability of getting at least 5 heads. We will use our function 100,000 times to find the probability.

np.mean(np.array([ten_fair() for i in range(100000)]))*100
62.307