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 .
-
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 .
-
Probability: This is the final result of the probability. The probability is commonly written as .
Formula to calculate the probability of a coin toss
=
Coin toss simulation
Let’s try the coin toss simulation using two methods:
- Using the
randommodule - Using binomial distribution
Method 1 (using the random module)
import randomdef coinToss():return random.choice(["Heads", "Tails"])for i in range(5):print coinToss()
Code explanation
-
Line 1:
import randomloads therandommodule that contains many random number generation functions. -
Line 3: A function named
coinToss()is defined. -
Line 4:
random.choicereturns a random value from the given list["Heads", "Tails"]. It either returnsHeadsorTailsrandomly every time this line is executed. -
Line 6-7: A
forloop is set up that callscoinToss()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:
Here, is the number of trials executed, is the number of successes, and 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 numpydef coinToss(p):prob = numpy.random.binomial(1,p)return probprobability = 0.5samplespace = ["Heads", "Tails"]for i in range(5):result = coinToss(probability)print samplespace[result]
Code explanation
-
Line 1:
import numpyloads thenumpymodule that contains thebinomialfunction. -
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 either0or1, which is stored in a variableproband returned. -
Line 7: The
probabilityvalue is set to0.5which is the probability of a coin toss. -
Line 8: An array named
samplespaceis 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 fromsamplespaceasHeadsorTails.
Free Resources