You are currently viewing Python Random Module Methods Explained

The Python random module is a built-in module of Python that provides several random methods for generating random or pseudo-random numbers. Using this module you can generate random numbers like integers, float numbers, and between two ranges of values etc.

Advertisements

In this article, I will explain the Python random module and use some of its methods to manipulate and generate random numbers in different ways.

1. List of Functions of Random Module in Python

Following is the list of Python random module methods, using these functions we can perform various actions randomly.

In order to use any of these functions, first you need to import the Python random module.

FunctionDescription
seed()Initialize the random number generator.
random()Generates a random float number between 0.1 and 1.0(exclude)
sample()Generates a random floating number from the given range of specified numbers.
uniform()Generates a random number between the given range.
randrange()Generates the random number between the given range with specified step.
randint()Generates the random number between the given range with specified steps.
choice()Generates a random element from the given sequence.
choices()Generates a list with a random selection from the given sequence.
shuffle()Allows a sequence and returns the sequence in a random order.
Python random module methods

Almost all module functions depend on the basic function random(), which is used to generate a random float number between the range of 0.0 to 1.0(exclude). The sequence of random number generated depends upon the seeding value of seed() function, which is another function of random module and is used to initialize the random number generator.

If you use the same seed value, you can generate the same sequence random number. For example,


import random
# Initialize the random number generator
random.seed(3)
print("Random numbers:")
print(random.random())
print(random.random())
print(random.random())
Python random module

1. Get a Random Float Numbers Between 0 to 1 using Module of Python

You can use a random.random() the function of a random module is one the most useful function for generating random numbers and is used to generate a random float number within a range of 0 to 1. Let’s use random() and get the random float number. Before going to use any function of random module we need to import random module.

Related: Generate Random Integers Between 0 and 9 in Python


# Import random module
import random
# Get the float random number between 0 to 1
print("Generated 3 times of random float number:")
for i in range(3):
  print(random.random())

# Output:
# Generated 3 times of random float number:
# 0.013167991554874137
# 0.83746908209646
# 0.25935401432800764

2. Get a Random Float Between a Range using Module Function

The random.uniform() from a random module is used to generate the random floating number from the given range of specified numbers. It takes lower and upper limit numeric values as its parameters so that a random floating number is generated within this specified range.

Let’s pass a specific range of integers(10,20) into random.uniform() to generate a single random floating number within a specific range.


# Import random module
import random
# Get the random float number using uniform()
# Intitialize the range limits
a = 10
b = 20
print("Lower limit:", a)
print("Higher limit:", b)
print("Random floating number:", rand_num)

# Output:
# Lower limit: 10
# Higher limit: 20
# Random floating number: 11.220084088874241

3. Get Random Integer using Module of Python

The randint() from a random module is used to generate the random integer from the given range of integers. It takes start(include) and end(include) numeric values as its parameters, so that a random integer is generated within this specified range.

Let’s pass a specific range of integers(20, 50) into random.randint(), it will generate a single random integer within a specific range.


import random
# Generate random integer using randint()
random_value= random.randint(20,50)
print("Random integer:", random_value)

# Output:
# Random Integer: 45

The randrange() from a random module is used to generate the random number between the given range with specified steps. It takes start and end(exclude) numeric values as its parameters so that a random number is generated within this specified range.

Let’s use randrange() and get the random integer between 1 to 10(exclude) with step value ‘2’.


import random
# Generate random number between 1 to 10
# using randrange()
random_value= random.randrange(1, 10, 2)
print("Random number between 1 to 10:", random_value)

# Output:
# Random number between 1 to 10: 3

4. Get Random Number from Sequence using Module of Python

The random.choice() is a part of random module of Python that returns a single random element from a sequence. It takes a sequence as its parameter and generates single random element from it.

You can use random.choice() function to generate a single random element from a range of integers from 0 to 9 sequences. range() function takes the start and end points and generates the sequence of numbers between them.

Let’s take a range(10) sequence and pass it into the choice() function to generate and return the single random element from the sequence. It is different for every execution. Before going to use this function we need to import a random module.


# Get random element using random.choice()
# Import random module
import random
rand_num = random.choice(range(10))
print("Random element:", rand_num )

# Output:
# Random element : 6

5. Shuffle the List of Elements Using Python Random Module

random.shuffle() from the built-in random module can be used to shuffle the order of the elements in the Python list. The shuffle() function takes a sequence, such as a list, and modifies it in place by shuffling the order of its elements randomly.

You can shuffle the list of elements using random.shuffle() function. Let’s pass the list into the shuffle() function to get the list with shuffled elements.


# Import random
import random

# Initialize list
mylist = [5, 10, 20, 30, 40, 50]
print("Original list: ", mylist)

# Shuffle list 
# Using random.shuffle() function
random.shuffle(mylist)
print("Shuffle list: ", mylist)

# Output:
# Original list:  [5, 10, 20, 30, 40, 50]
# Shuffle list:  [30, 40, 10, 50, 20, 5]

7. Conclusion

In this article, I have explained about Python random module and using some of the functions of the module how we can perform various actions randomly, such as generating random numbers like integers, floats, and selecting selections from the sequence, and shuffling the elements of a given sequence randomly.

References