Python – Generate a random number

Table of Contents

Introduction

Are you learning Python and you need to know how to generate a random number? Well, you are in the right place!

This post will show you how to generate one or a list of random numbers in Python using the random module.

How to generate a random number between 0 and 1

To generate a random number between 0 and 1 you can use the following code:

import random
random_number = random.random()
print(random_number)

Output

0.1158682742815027

This code is quite straightforward, it simply uses the random function to generate a random number between 0 and 1.

If you repeat this code several times, the generated output will be always different.

Execution of several random numbers generation
Execution of several random numbers generation

How to generate a random number in a range

In the previous section we have seen how to genereate a random number between 0 an 1, but how to use a different range?

random.uniform(a,b)

Using uniform function you can choose the range but remember, the result will be always a float number.

import random
# generate a random number bewteen 10 and 30
random_number = random.uniform(10, 30)
print(random_number)

Output

19.98978065463152

random.randrange(start, stop[, step])

Another option is to use the randrange function:

import random
# generate a random number bewteen 10 and 30
random_number = random.randrange(10, 31)
print(random_number)

Output

27

Please note that the returned number is not a float like the other cases, but it is an integer and that the upper limit is not inclusive.
Indeed to generate a random number between 10 and 30 we need to use 10 and 31.

Generate a list of random numbers

We have seen how to generate a single random number but how to generate a list?
First of all, if you haven’t already, check out this tutorial on Python lists.

In the code below we add 10 random numbers to a list using a for loop.

import random
random_numbers_list = []
for i in range(10):
    random_numbers_list.append(random.random())
print(random_numbers_list)

Output

[
    0.4417978397617499, 0.8503475562767052, 0.9076880408916723, 0.3478745444272511, 0.798930033913547, 0.31536954901543124, 
    0.21430172365366318, 0.742072649496668, 0.9523878916474506, 0.27508925158450315
]

Keep in mind that you can use any other function showed in this post.
In this way you can generate not only numbers between 0 and 1 but also in another range!

Extract random number from a sequence

For this kind of task the choice function is perfect!

import random
my_list = ["a", "b", "c", "d"]
# extract random choice from sequence
random_choice = random.choice(my_list)
print(random_choice)

Output

c

Please remember that if the sequence is empty an IndexError exception is raised.

If you want more than one element you can use the choices function.

import random
my_list = ["a", "b", "c", "d"]
# extract 3 random choices from sequence
random_choices_list = random.choices(my_list, k=3)
print(random_choices_list)

Output

['a', 'd', 'c']

Conclusion

I hope this post was useful to you.
We have seen how to generate a random number using Python and how to extract choices from a sequence like a list.

As usual leave me a feedback in the comments and let me know if you are in troubles!

Leave a Comment

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