MathProbabilityProbabilistic modeling

Random experiments

2 minutes read

How can you ensure that a die is fair? Apparently, the most straightforward way is to roll the die many times and then check if all the results have come up approximately the same number of times. Similar logic can be applied to many random phenomena in real life. In this topic, we will learn how to analyze such phenomena using a large set of their results. And to avoid "rolling the die" numerous times ourselves, we will use basic Python and the random module.

Random experiments

In probability theory, a random experiment is a procedure with the following particular aspects:

  1. It can be repeated an infinite number of times under the same conditions.

  2. The outcome of this procedure cannot be predicted in advance.

Repetitions of the same experiment under the same conditions are called trials. So, when we flip a coin four times, we may interpret this as one trial of the experiment "flip a coin four times" or as four trials of an experiment "flip a coin".

A random experiment is a mathematical model. No phenomenon in real life can truly be a random experiment, simply because nothing can be repeated an infinite number of times.

Despite its mathematical nature, the model of a random experiment can accurately describe many real-world phenomena: flipping a coin, rolling a die, drawing the top card from a freshly shuffled deck. It turns out that the concept of a random experiment can even be applied to deterministic sequences, for which there exists an algorithm that computes all their elements. Such algorithms are called random number generators and are widely used in data analysis. Roughly speaking, a random number generator is a program that takes a non-negative integer called a seed and produces a unique, deterministic sequence of numbers of the desired length. The point is that this sequence cannot be distinguished from a truly random sequence if the seed is not known. This is very helpful in studies where we want to incorporate randomness while also ensuring our study is reproducible, meaning that anyone can repeat our actions and get the same results.

The more times we roll the die, the more accurately we can determine how close the fractions of all outcomes are to one another. However, to prove that these fractions are exactly equal, we would need to roll the die an infinite number of times. That is why the ability to conduct an infinite number of trials is crucial: without it, we wouldn't even be able to define the concept of a "fair die"!

Conducting many trials on our own is time-consuming and tedious. This task can be done much faster and more easily by writing a program!

Random experiments in Python

There are many ways to conduct random experiments in Python. In this topic, we will be using the random module. For example, to get a random element from a given list a, you may execute the following code:

import random

a = ['Head', 'Tail']
print(random.choice(a))
Head

To run this experiment k times, you need to execute the corresponding function k times:

a = ['Head', 'Tail']
k = 6
print([random.choice(a) for i in range(k)])
['Head', 'Tail', 'Head', 'Head', 'Tail', 'Head']

Alternatively, you can use the method random.choices, which takes k as a parameter:

print(random.choices(a, k=6))
['Tail', 'Head', 'Head', 'Tail', 'Tail', 'Head']

If you want to pick a random integer from range(a, b, step), you can use random.randrange(a, b, step) instead of random.choice(range(a, b, step)):

def dice_roll():
    return random.randrange(1, 7)

print([dice_roll() for i in range(5)]) # let's roll the dice 5 times
[5, 3, 4, 5, 3]

More complex experiments

Using basic experiments, we can build complex experiments. For example, an experiment where we flip a coin until the second head appears can be conducted as follows:

def coin_flip():
    return random.choice(['H', 'T'])

def flip_until_second_head():
    total_heads = 0
    outcome = []
    while total_heads < 2:
        outcome.append(coin_flip())
        if outcome[-1] == 'H':
            total_heads += 1
    return outcome

# Let's conduct the experiment 10 times
for i in range(10):
    print(flip_until_second_head())
['T', 'T', 'T', 'T', 'H', 'H']
['H', 'T', 'H']
['T', 'T', 'T', 'T', 'T', 'H', 'T', 'T', 'H']
['H', 'T', 'T', 'T', 'T', 'H']
['H', 'H']
['H', 'T', 'T', 'H']
['H', 'T', 'H']
['H', 'H']
['T', 'T', 'T', 'T', 'T', 'T', 'H', 'H']
['H', 'T', 'T', 'H']

Its outcomes are not numbers, but sequences of heads and tails of different lengths!

Conclusion

Let's sum up this topic:

  • To ensure fair outcomes from a die, it requires multiple rolls to check for an even distribution of results.

  • In probability theory, random experiments model unpredictable procedures that can be repeated under the same conditions.

  • Python's random module allows for efficient execution of such experiments.

  • Conducting more trials increases the accuracy of assessing fairness, which is crucial for defining a "fair die."

To discuss even such complex experiments, we need to introduce the concept of events. So, please be patient until the next topic.

How did you like the theory?
Report a typo