How to generate a random number between 0 and 7 using a coin

Generate a random number

While generating a random number from a given range, we must ensure that all numbers have an equal likelihood of being generated. For example, when generating a random number between 00 and 77, each of the numbers 0,1,2,3,4,5,6,0, 1, 2, 3, 4, 5, 6, and 77 has a 18\frac{1}{8} or 0.1250.125 chance of being generated.

We can think of a coin as a random number generator. Flipping a coin only has two outcomes, heads or tails, each with a probability of 0.50.5.

To use a coin to generate a number between 00 and 77, we must first understand the binary representation of numbers.

Binary number system

Numbers in our everyday life are usually in the decimal or base-10 system because each digit can have one of ten values (090–9). In the binary or base-2 system, each digit can only have one of two values, 00 or 11.

We can use at least three digits to represent all the numbers in the range [0,7][0,7].

Binary Representation of Numbers

Base-10

Base-2

0

000

1

001

2

010

3

011

4

100

5

101

6

110

7

111

Note: Click here to learn how to convert decimal and binary values.

As we can see, a base-10 number can be represented as a sequence of 1s and 0s. If we have to generate a base-10 number between 00 and 77, we can generate three binary numbers instead.

Luckily, we have a random binary generator, our coin!

Example

If we flip the coin three times and note the results as 00 or 11, we get a three-digit binary number. Once we convert this binary number to base-10, we'll get a random number between 00 and 77.

The simulation below demonstrates the process:

Generating a random number between 0 and 7 using three coin flips

Equal probabilities

As we stated before, any number between 070–7 should appear with the probability of 0.1250.125.

Three consecutive coin flips have the following possible outcomes. An 'H' represents heads while a 'T' represents tails.

Outcomes

Possible results

Base-2

Base-10

HHH

000

0

HHT

001

1

HTH

010

2

HTT

011

3

THH

100

4

THT

101

5

TTH

110

6

TTT

111

7

Note: We treat a heads as 0 and a tails as 1.

Any single outcome has a 1 in 8 chance of occurring. Hence each number is equally likely.

We can use a coin can to generate a random number between any range [0, 2n1][0,\ 2^n-1]where n1n\geq1. The number of coin-flips and digits of the resulting binary number would be nn. With n=3n=3 we generated a number in [0,7][0, 7] with 33 coin-flips and a 33 digit resultant binary number.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved