# QWorld Bronze: Coin Flip

(01/06/2021)

## Coin Flip: A Probabilistic Bit

## A Fair Coin

A coin has two sides: Heads and Tails. After flipping a coin, we can get Heads or Tails. We can represent these two different cases by a single bit:

- 0 represents Heads
- 1 represents Tails

Flipping a fair coin If our coin is fair, then the probabilities of getting Heads and Tails are equal:

$p= \dfrac{1}{2} = 0.5$

Flipping a fair coin can be represented as an operator:

$FairCoin(Tails) \ = \frac{1}{2} Heads + \frac{1}{2}Tails$

Or, by using 0 and 1:

$\begin{array}{c|cc} & \mathbf{Heads} & \mathbf{Tails} \\ \hline \mathbf{Heads} & \dfrac{1}{2} & \dfrac{1}{2} \\ \mathbf{Tails} & \dfrac{1}{2} & \dfrac{1}{2} \end{array}$

## Task 1: Simulating FairCoin in Python

- Flip a fair coin 100 times. Calculate the total number of heads and tails, and the n check the ratio of the number of heads and the number of tails.
- Do the same experiment 1000 times.
- Do the same experiment 10,000 times.
- Do the same experiment 100,000 times.
- Do your results get close to the ideal case (the numbers of heads and tails are equal)?

from random import randrange # # you may use method 'randrange' for this task # randrange(n) returns a value from {0,1,...,n-1} randomly ## # your solution is here #

## Flipping a biased coin

Our coin may have a bias.

For example, the probability of getting heads is greater than the probability of getting tails.

Here is an example:

$BiasedCoin = \begin{array}{c|cc} & \mathbf{Head} & \mathbf{Tail} \\ \hline \mathbf{Head} & 0.6 & 0.6 \\ \mathbf{Tail} & 0.4 & 0.4 \end{array}$

Or, by using 0 and 1 as the states:

$BiasedCoin = \begin{array}{c|cc} &0 & 1\\ \hline 0 & 0.6 & 0.6 \\ 1 & 0.4 & 0.4 \end{array}$

## Task 2: Simulating BiasedCoin in Python

Flip the following biased coin 100 times. Calculate the total numbers of heads and tails, and then check the ratio of the number of heads and the number of tails.

- Do the same experiment 1000 times.
- Do the same experiment 10,000 times.
- Do the same experiment 100,000 times.
- Do your results get close to the ideal case?