In this article, I will explain the usage of the random
module in Python. As the name implies it allows you to generate random numbers.
This random module contains pseudo-random number generators for various distributions.
The function random()
is one of them, it generates a number between 0 and 1.
But there are other like the functions randint(min,max)
and randrange(max)
.
Related Course: Complete Python Programming Course & Exercises
Introduction
Lets start with the absolute basic random number generation. The function random.random()
.
The function random()
returns the next random float in the range [0.0, 1.0].
To use the random()
function, call the random()
method to generate a real (float) number between 0 and 1.import random
x = random.random()
print(x)
This outputs any number between 0 and 1. For most apps, you will need random integers instead of numbers between 0 and 1.
Generate random numbers
The function randint()
generates random integers for you. If you call the function, it returns a random integer N
such that a <= N <= b
.
The randint()
method to generates a whole number (integer). You can use randint(0,50)
to generate a random number between 0 and 50.
import random |
To generate random integers between 0 and 9, you can use the function randrange(min,max)
.
from random import randrange |
You can use randint(min,max)
instead:
import random |
Change the parameters of randint() to generate a number between 1 and 10.
import random |
Related Course: Complete Python Programming Course & Exercises
List of random numbers
If you want to generate a list of random number, you can do so by using a for loop.
To generate a list of 100 random numbers:import random
mylist = []
for i in range(0,100):
x = random.randint(1,10)
mylist.append(x)
print(mylist)
But this can be done in a much more compact way in Python, with a one liner.
The function to use is sample()
which shuffles the input list, in the example below it shuffles the created list range(1,101)
.
That is to say, range(1,101)
creates a list of numbers 1 to 100.
>>> list(range(1,101))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
Then the function sample()
shuffles that list in random order.
import random |
Choosing random items from a list
You can use the sample()
method to put the list in a random order. But you can also use it get random items from a list.
If you want 3 random items from the list, you add 3
as second parameter of the sample(list)
method.import random
mylist = [1,2,3,4,5,6,7,8,9,10]
x = random.sample(mylist,3)
print(x)
If you want to pick a random item, you can use the choice(list)
method. But this returns only one element.
import random |
You can use the method shuffle(list)
to shuffle the list order and then use the first index as random number.
import random |
The recommended way to do this is using the choice()
method, but all of these work.
If you are a Python beginner, then I highly recommend this book.