Tutorials Tonight
Follow Us

How To Generate Random Number In Python


In this tutorial, you will learn how to generate random number in Python. You will also see variations of this technique like getting a single random number, getting a range of random numbers, selecting random numbers from a list, etc.

There are situations when you need some random numbers to test your program or to check the behavior of a model. In such a scenario, you need a random number to perform the task.

Python can generate random numbers for this using random module.

The random module has lots of function that help us with different random number generation tasks.

Let's start with the simplest example of random number generation.

generate random number in python

    Table Of Contents

  1. Generate Random Number Between 0 and 1
  2. Generate Random Number In Range
    1. Using randint() function
    2. Using uniform() function
    3. Custom function
  3. Generate Random List Of Numbers
  4. Choose Random Element From List
  5. Conclusion

Generate Random Number Between 0 and 1

The random module has a function called random() that generates a random number between 0 and 1.

The generated number is a floating-point number with a precision of about 16 decimal places.

The following code generates a random number between 0 and 1:

import random

# number between 0 and 1
# using random() function
num = random.random()

print(num)

Output: (may vary)

0.8962432886855236

Note: The generated random number changes every time you run the code.

When you have a random number between 0 and 1, then you can generate a random number between any two numbers. We will see how to do this later in this tutorial.


Generate Random Number In Range

There can be multiple different ways to get a random number in a range. Here we are going to see 3 different ways of this.

1. Using randint() Function

The function randint() generates a random number (integer) between two numbers.

The function takes a minimum and maximum number as arguments and returns an integer value between these 2 numbers including the minimum and maximum number.

Let's see an example of this:

import random

# number between 0 and 10
num = random.randint(0, 10)
print(num)

# number between 10 and 20
num = random.randint(10, 20)
print(num)

Output: (may vary)

5
15

2. Using uniform() Function

Just like randint() function the uniform() generates a random number between two numbers, but instead of generating an integer value, it generates a floating-point value.

It also accepts 2 arguments, minimum and maximum numbers for range. The generated number may or may not include the minimum and maximum number depending on the random number generated.

Creating a floating-point random number in range using uniform() function:

import random

# number between 0 and 10
num = random.uniform(0, 10)
print(num)

# number between 10 and 20
num = random.uniform(10, 20)
print(num)

Output: (may vary)

3.964394758422425
14.501730515775982

3. Creating Own Custom Function

We mentioned earlier that if you have a random number between 0 and 1 then you can generate a random number between any given range.

Let's create our own function to generate random numbers in a range.

For this, we will use random.random() function to generate a random number between 0 and 1. And then we will expand this random number to a range.

Let's see the program first then we will see how the function works.

import random

# custom function for random number in range
def random_number_in_range(min, max):
    # number between 0 and 1
    r = random.random()

    # expand the number to a range
    num = r * (max - min) + min
    return num

# number between 0 and 10
num = random_number_in_range(0, 10)
print(num)

# number between 10 and 20
num = random_number_in_range(10, 20)
print(num)

Output: (may vary)

6.328116209039756
15.00905517135427
random number in the range

Generate Random List Of Numbers

Until now we have generated only single random numbers. Let's generate a list of random numbers.

import random

size = 10
list = []

# create a list of random numbers
for i in range(size):
    num = random.randint(0, 100)
    list.append(num)

print(list)

Output: (may vary)

[62, 51, 4, 98, 57, 20, 98, 73, 20, 80]

Choose Random Element From List

The choice() function is used to choose a random element from a list.

It takes a list as an argument and returns a random element from the list.

import random

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# choose a random element from the list
num = random.choice(list)
print(num)

Output: (may vary)

6

Conclusion

The random module is used for any random task, whether it is to generate a random number, select a random element from a list, shuffle a list, etc.

Here we have seen how to generate a random number in Python with multiple useful examples.

Reference: random module