Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a random number generator in Python

Understanding Randomness

Before we dive into the world of Python and learn how to create a random number generator, let's first understand the concept of randomness. Imagine you're tossing a coin. You can't predict whether it will land on heads or tails. That's randomness! Similarly, a random number generator will produce a sequence of numbers that cannot be reasonably predicted.

Random Number Generation in Python

Python, a powerful and versatile programming language, has a built-in module called random that can be used to generate random numbers. This is like a magician's box full of different tricks that can help you create randomness in your programs.

Starting Your Python Journey: Importing the Random Module

Our first step will be to summon Python's random module. It's like calling out to a friend for help. Here's how you do it:

import random

With this line of code, all the functions inside the random module are ready to assist us.

The Simplest Random Number: random.random()

The most straightforward way to generate a random number in Python is by using the random() function. This function returns a random float number between 0.0 and 1.0. Think of it as reaching into a bag filled with numbers from 0.0 to 1.0, and pulling one out with your eyes closed.

Here's how to use it:

import random
print(random.random())

Every time you run this code, you'll get a different number (between 0.0 and 1.0).

random.randint(): For Whole Numbers

What if we need a whole number, not a float? Python provides the randint() function. It returns a random integer within the range you specify. It's like having a bag of numbers within a certain range and randomly picking one. Here is how to generate a random integer between 1 and 10:

import random
print(random.randint(1, 10))

random.uniform(): For a Wider Range of Floats

The uniform() function is like an upgraded version of random(). It allows you to specify a range, but unlike randint(), this range can include float numbers. It's like expanding your bag to include a wider range of numbers. Here is an example:

import random
print(random.uniform(1.5, 5.5))

This will generate a random float number between 1.5 and 5.5.

random.choice(): To Choose Randomly from a List

Python's random module is not just limited to generating random numbers. We can also select a random element from a list using random.choice(). Imagine having a bowl of names written on pieces of paper and randomly picking one. That's what random.choice() does.

Here is an example:

import random
my_list = ['apple', 'banana', 'cherry']
print(random.choice(my_list))

random.shuffle(): To Shuffle Elements

What if you want to mix up the elements in your list? random.shuffle() is here to the rescue. It's like taking a deck of cards and shuffling it. Here is how you do it:

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

Conclusion: The Power of Randomness

Congratulations! You've just learned how to generate randomness in Python. With these new tools in your programming toolkit, you're now capable of making your programs unpredictable and dynamic. Remember, randomness can make a game more fun, a simulation more realistic, or even a machine learning algorithm more robust. So, don't underestimate the power of randomness. Happy coding!