How to find the coin toss probability in Python

Probability is simply the chance of the occurrence of an event. For example, tossing a fair coin has only two possibilities. Either heads or tails will come on top of the coin. The chance of either heads or tails is 50% or 0.5 (on a scale of 0-1).

Note: Commonly, the probability is scaled from 0 to 1.

Probability of a coin toss

The occurrence of getting either heads or tails in a fair, unbiased coin toss is always 0.5.

Important terms

  • Experiment: This is the case study under consideration. For example, the coin toss is the experiment in our case.

  • Sample Space: This is the total number of possible outcomes in an experiment. For example, the total possible outcomes in a coin toss experiment is 2, either heads or tails. Sample space is commonly written as SS.

  • Event: This refers to a particular case in an experiment. For example, getting heads on the face of the coin is an event. The event is commonly written as EE.

  • Probability: This is the final result of the probability. The probability is commonly written as PP.

Formula to calculate the probability of a coin toss

S(SampleSpace)=2S (Sample Space) = 2

E(Event:Gettinghead)=1E (Event: Getting head) = 1

P(E)=ESP(E) = \frac{E}{S} = 12=0.5\frac{1}{2} = 0.5

Coin toss simulation

Let’s try the coin toss simulation using two methods:

  1. Using the random module
  2. Using binomial distribution

Method 1 (using the random module)

import random
def coinToss():
return random.choice(["Heads", "Tails"])
for i in range(5):
print coinToss()

Code explanation

  • Line 1: import random loads the random module that contains many random number generation functions.

  • Line 3: A function named coinToss() is defined.

  • Line 4: random.choice returns a random value from the given list ["Heads", "Tails"]. It either returns Heads or Tails randomly every time this line is executed.

  • Line 6-7: A for loop is set up that calls coinToss() five times, and a random value from ["Heads", "Tails"] is returned every time.

Method 2 (using binomial distribution)

In probability theory, binomial distribution is a probability distribution that gives only two possible results, either success or failure.

It uses the following equation:

P(N)=(nN)pN(1p)nNP(N) = \binom{n}{N} p^N (1-p)^{n-N}

Here, nn is the number of trials executed, NN is the number of successes, and pp is the probability.

In Python, the function used for binomial distribution is numpy.random.binomial(n,p). It comes from the numpy module’s random class. It takes two parameters. One is n which is the number of trials, and the second is p which is the probability of the event under consideration.

import numpy
def coinToss(p):
prob = numpy.random.binomial(1,p)
return prob
probability = 0.5
samplespace = ["Heads", "Tails"]
for i in range(5):
result = coinToss(probability)
print samplespace[result]

Code explanation

  • Line 1: import numpy loads the numpy module that contains the binomial function.

  • Line 3: A function named coinToss() is defined that takes the probability as a parameter.

  • Lines 4-5: The function numpy.random.binomial(1,p) takes two parameters, the first is the number of trials and the second is the probability value. It then generates either 0 or 1, which is stored in a variable prob and returned.

  • Line 7: The probability value is set to 0.5 which is the probability of a coin toss.

  • Line 8: An array named samplespace is created to store heads and tails which can be displayed to the user.

  • Lines 10-12: A loop is set up that executes five times and calls the coinToss() function and displays the result from samplespace as Heads or Tails.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved