Generate random numbers using Python random module

Random number generation is a fundamental concept in programming, essential for various tasks such as statistical sampling, simulation, data generation, randomized algorithms, and even games.

In this tutorial, we will cover the basic concepts, how to generate different types of random numbers (like integers, floats, and within a specific range), and the various functions provided by the random module.

The tutorial is intended for anyone with a basic understanding of Python, interested in understanding random number generation and its applications.

 

 

What algorithm does the random module use?

Python uses a popular and robust algorithm called the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1.

print(random.random())

Output:

0.7345120869049328 (Note: Your number will vary.)

The random() function returns a random float number between 0 and 1.

 

Generating integers with random.randint()

The randint() function from random module generates a random integer within the specified range. This function takes two arguments: the start and the end of the interval.

print(random.randint(1, 10))

Output:

5 (Note: Your number will vary.)

This code outputs a random integer between 1 and 10, inclusive.

 

Generating floats with random.random()

This function returns a random floating-point number in the range [0.0, 1.0).

print(random.random())

Output:

0.8722616472784621 (Note: Your number will vary.)

The random() function doesn’t take any arguments. It returns a random float number between 0 (inclusive) and 1 (exclusive). This number can be scaled to the desired range if needed.

 

Generating floats between a specific range

We can generate a random float number within a specific range [a, b) by multiplying the output of random() by (b-a) and adding a to it.

a, b = 5, 10
random_number = a + (b - a) * random.random()
print(random_number)

Output:

7.361308323912307 (Note: Your number will vary.)

The result is a random float number between 5 and 10. This is useful when we need more control over the range of random floats.

 

Picking a random element from a list

The random.choice() function returns a random element from the non-empty sequence.

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
print(random.choice(fruits))

Output:

'cherry' (Note: Your selection will vary.)

In this instance, we are selecting a random element from a list of fruits. The random.choice() function will randomly choose one item from the list each time it’s called.

 

Picking multiple unique elements

The random.sample() function returns a random sample from a sequence without replacement. This means that each selected element is removed from the original list, ensuring no element is selected twice.

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
print(random.sample(fruits, 3))

Output:

['banana', 'elderberry', 'date'] (Note: Your selection will vary.)

Here, we are selecting three unique elements from our list of fruits.

 

Generating Random Binary and Hexadecimal Values

We can generate random binary and hexadecimal values using the getrandbits() function.

print(bin(random.getrandbits(5)))
print(hex(random.getrandbits(5)))

Output:

0b1001
0x3 (Note: Your output will vary.)

The first line generates a random binary value with a length of 5 bits, while the second line generates a random hexadecimal value with the same bit length.

 

Shuffling a list in-place

The random.shuffle() function randomly reorders the elements in a list.

numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)

Output:

[3, 2, 4, 1, 5] (Note: Your order will vary.)

Here, we have a list of numbers from 1 to 5. After we shuffle the list, the order of the numbers has changed randomly.

 

Reproducing the same sequence

The random.seed() function customizes the start number of the random number generator. Given the same seed, the generator will produce the same sequence of numbers, which is useful in testing and debugging.

random.seed(0)
print([random.random() for _ in range(3)])
random.seed(0)
print([random.random() for _ in range(3)])

Output:

[0.8444218515250481, 0.7579544029403025, 0.420571580830845]
[0.8444218515250481, 0.7579544029403025, 0.420571580830845]

We are capturing the current internal state of the random number generator by using a seed value of 0. We then generate the same sequence of three random numbers twice by resetting the seed to 0 before each sequence.

 

Pseudo-randomness vs. True randomness

In Python, the random module generates pseudo-random numbers. This means they are generated by a deterministic process, but they satisfy certain statistical properties that make them seem random.

True randomness, on the other hand, is non-deterministic and impossible to reproduce exactly.

random.seed(5)
print(random.random())
random.seed(5)
print(random.random())

Output:

0.6229016948897019
0.6229016948897019

When we use the same seed, the random number generator produces the same sequence of numbers, which shows that it is deterministic and not truly random.

 

Applications of Random Numbers

Random numbers are used extensively in programming for games, simulations, testing, security, and privacy.

For example, in games and simulations, we often want to introduce some sort of randomness in the environment.

In testing, we often use random inputs to test the robustness and correctness of our code. In security and privacy, we often use random numbers to generate keys and tokens that protect our data.

 

Random Strings and Passwords

We can generate random strings and passwords using a combination of the random and string modules.

import random
import string
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(15))
print(password)

Output:

alYO\vJJT0B@g$b (Note: Your output will vary.)

Here, we have combined all ASCII letters (both lowercase and uppercase), digits, and punctuation.

Then, we used a list comprehension with the random.choice() function to pick 15 random characters and join them into a string.

 

Generating random data for simulations

Random numbers can be used to generate synthetic datasets for simulations or for testing the performance of algorithms.

For example, we might want to generate a dataset of random ages between 0 and 100 for a simulation of a population.

ages = [random.randint(0, 100) for _ in range(1000)]
print(ages[:10])

Output:

[85, 24, 68, 80, 51, 25, 42, 97, 3, 7] (Note: Your output will vary.)

This code generates a list of 1000 random ages between 0 and 100. We print out the first 10 to verify.

 

A/B Testing with Python Random Module

A/B testing is a common method used to compare two versions of a webpage or other user experience to determine which one performs better.

Random numbers can play an essential role in A/B testing by randomly assigning users to one group or the other.

We will use the random module to simulate the assignment of users to different versions of a website:

def assign_to_group(user_id):
    if random.random() < 0.5:
        return 'Group A'
    else:
        return 'Group B'

# Simulate assigning 10 users to a group
for user_id in range(1, 11):
    group = assign_to_group(user_id)
    print(f"User {user_id} is assigned to {group}")

Output:

User 1 is assigned to Group B
User 2 is assigned to Group A
User 3 is assigned to Group B
User 4 is assigned to Group B
User 5 is assigned to Group A
User 6 is assigned to Group A
User 7 is assigned to Group B
User 8 is assigned to Group B
User 9 is assigned to Group B
User 10 is assigned to Group A

In this code, we’ve simulated assigning users to one of two groups, Group A or Group B, for A/B testing. We use the random.random() function to generate a random float between 0.0 and 1.0.

If the number is less than 0.5, we assign the user to Group A; otherwise, we assign them to Group B.

By running this function for a range of user IDs, we simulate the random assignment of users to different test groups.

By randomly assigning users to each group, we ensure that each user has an equal chance of being in either group, reducing bias in our A/B testing.

 

Further Reading

https://docs.python.org/3/library/random.html

Leave a Reply

Your email address will not be published. Required fields are marked *