
Understanding Data Types – A Guide for Entry Level Data Analyst
May 27, 2025
Unlocking Insights with Data Visalization using Matplotlib and Seaborn
July 22, 2025Let’s take a look at random number generation in base Python. In the early days of my Master of Data Science program, I was exposed to the concept of randomness. I found it fascinating, so I want to share my notebook and the lessons I learned in that class.
What is Randomness in Python and Why Randomness?
According to John V. Guttag in his book “Introduction to Computation and Programming Using Python: With Application to Understanding Data (2nd edition)” the random module, provides functions for generating pseudo-random numbers. These numbers are not truly random, but they are generated using a deterministic algorithm that makes them appear random for most practical purposes. This means that Randomness in Python is the concept of generating unpredictable and non-deterministic values using the random
module. This module offers functions to produce random numbers, shuffle sequences, and make random selections, which are crucial for various computational tasks such:
- Simulating coin flips: The random.choice([‘heads’, ‘tails’]) function could be used to simulate the outcome of a coin toss.
- Modeling random walks: You could use random.random() to generate a random number between 0 and 1, and then use that number to decide whether to take a step forward or backward in a simulation.
- A Sample from a Gaussian Distribution: The random.gauss(mu, sigma) function could be used to generate random numbers that follow a normal distribution with a given mean (mu) and standard deviation (sigma).
Why Randomness?
Understanding randomness in Python allows you to create more dynamic, fair, and secure data analysis, applications, enhancing their overall functionality and reliability for applications including:
- Simulations: Randomness is vital for simulations in fields like physics, biology, and finance, where it helps model real-world phenomena and uncertainties.
- Games and Entertainment: Random elements make games more engaging and unpredictable, providing a unique experience every time.
- Statistical Sampling: Random sampling is essential for unbiased data analysis and accurate representation of larger populations.
- Cryptography: Secure random numbers are fundamental for encryption, ensuring data security and privacy.
- Testing: Randomized testing can help identify edge cases and improve the robustness of software by covering a wide range of inputs.
Application of Randomness in Base Python
Randomness in base Python is achieved using the random module, which provides a suite of functions to generate random numbers, select random items from a list, and perform other random operations. This module is essential for various applications, including simulations, games, testing, and cryptography. By enabling the generation of random data, the random module helps in creating unpredictable outcomes, essential for modeling real-world uncertainties and ensuring fairness in processes like randomized trials. Understanding and utilizing randomness in Python allows developers to introduce variability and test scenarios in their code, making it robust and versatile for diverse applications Let’s dive into my Jupyter Notebook application to see how to generate random numbers using random.random(). You will see that it is important to control your randomness as it sets the range of the target numbers to generate, and we will do some normal distribution simulation to compute the Standard Error to the Mean of approximately 5000 mean replications.
This notebook will covers, Importing the random module, Calling random functions from a module and Controlling Randomness
Importing Module
import random
You can see more information on the random specific method by calling the help function
help(random.random)
Help on built-in function random:
random() method of random.Random instance
random() -> x in the interval [0, 1).
help(random.seed)
Help on method seed in module random:
seed(a=None, version=2) method of random.Random instance
Initialize internal state from a seed.
The only supported seed types are None, int, float,
str, bytes, and bytearray.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
If *a* is an int, all bits are used.
For version 2 (the default), all of the bits are used if *a* is a str,
bytes, or bytearray. For version 1 (provided for reproducing random
sequences from older versions of Python), the algorithm for str and
bytes generates a narrower range of seeds.
Call a Random Number
random.random()
0.9886081659083231
Controlling Randomness
We control randomness in computer programs by setting random seeds
Using the random.seed() function to SET and DEFINE the seed that controls the random number generator
random.seed(20)
print(random.random())
print(random.random())
0.9056396761745207
0.6862541570267026
This is designed to show how to control the randomness.
By defining how the randomness is reproduced.
# The seed defines how the randomness is produced in the variable x_a
random.seed(100)
x_a = [ random.random(), random.random(), random.random() ]
x_a
[0.1456692551041303, 0.45492700451402135, 0.7707838056590222]
# The seed defines how the randomness is produced in the variable x_b
random.seed(100)
x_b = [ random.random(), random.random(), random.random() ]
# The seed defines how the randomness is produced x_c
random.seed(100)
x_c = [ random.random(), random.random(), random.random() ]
# Comparing the variables notice the all have 100 seeds
x_a == x_c == x_b
True
# Note that to check the objects true variance
x_a is x_b
False
x_a is x_c is x_b
False
random.seed(1000)
random.random()
0.7773566427005639
random.random()
0.6698255595592497
random.random()
0.09913960392481702
random.random()
0.35297051119014544
random.random()
0.4679077429008419
random.random()
0.5346837414708775
Key take away
Here we can see that randomness is ideally used for simulation understanding of randomness in Python, The random
module specifically focuses on random number generation and simulation techniques. However, there are other easier-to-use random sequence generators available, thanks to modules like Numpy and Pandas. Next, we will explore simulating a normal distribution using the random module and some user-defined functions. Keep coding…!