Open In App

Python Random Module

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python Random module generates random numbers in Python. These are pseudo-random numbers means they are not truly random.

This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc. It is an in-built function in Python.

Applications of Python Random Module

Here are some real-life uses of Python’s random module:

  1. Creating dice rolls or card draws in games.
  2. Simulating random events in traffic or stock market models.
  3. Selecting random samples from survey data.
  4. Splitting datasets into training and testing sets in machine learning.
  5. Generating random passwords or session tokens for security.
  6. Creating fake test data for software testing.
  7. Stress testing apps with random inputs.

List of all the functions  Python Random Module

There are different random functions in the Random Module of Python. Look at the table below to learn more about these functions:

Function Name

Description

seed()Initialize the random number generator
getstate()Returns an object with the current internal state of the random number generator
setstate()Used to restore the state of the random number generator back to the specified state
getrandbits()Return an integer with a specified number of bits
randrange()Returns a random number within the range
randint()Returns a random integer within the range
choice()Returns a random item from a list, tuple, or string
choices()Returns multiple random elements from the list with replacement
sample()Returns a particular length list of items chosen from the sequence
random()Generate random floating numbers
uniform()Return a random floating number between two numbers both inclusive
triangular()Return a random floating point number within a range with a bias towards one extreme
betavariate()Return a random floating point number with beta distribution
expovariate()Return a random floating point number with exponential distribution
gammavariate()Return a random floating point number with a gamma distribution
gauss()Return a random floating point number with Gaussian distribution
lognormvariate()Return a random floating point number with a log-normal distribution
normalvariate()Return a random floating point number with normal distribution
vonmisesvariate()Return a random floating point number with von Mises distribution or circular normal distribution
paretovariate()Return a random floating point number with a Pareto distribution
weibullvariate()Return a random floating point number with Weibull distribution

Random Module in Python Examples

Let’s discuss some common operations performed by Random module in Python.

Printing a random value from a list in Python.

This code uses the random module to select a random element from the list q using the random.choice() function. It prints a random element from the list, demonstrating how to pick a random item from a sequence in Python.

Python
import random
a = [1, 2, 3, 4, 5, 6]
print(random.choice(a))

Output
3

Creating random numbers with Python seed() in Python.

Random numbers depend on the seeding value. For example, if the seeding value is 5 then the output of the below program will always be the same. Therefore, it must not be used for encryption.

The code sets the random number generator’s seed to 5 using random.seed(5). It then prints two random floating-point numbers between 0 and 1 using random.random(). The seed makes these numbers the same every time you run the code with a seed of 5.

Python
import random
random.seed(5)
print(random.random())
print(random.random())

Output
0.6229016948897019
0.7417869892607294

Generate Random Numbers in Python

random.randint() method is used to generate random integers between the given range.

Syntax: randint(start, end)

Example: Creating random integers

It first generates a random integer between 5 and 15 (inclusive) and then between -10 and -2 (inclusive). The generated integers are printed with appropriate formatting.

Python
import random
r1 = random.randint(5, 15)
print("Random number between 5 and 15 is % s" % (r1))

r2 = random.randint(-10, -2)
print("Random number between -10 and -2 is % d" % (r2))

Output
Random number between 5 and 15 is 10
Random number between -10 and -2 is -2

Generate Random Float numbers in Python

A random.random() method is used to generate random floats between 0.0 to 1.

Syntax: random.random()

Example:

In this code, we are using the random function from the ‘random’ module in Python. It prints a random floating-point number between 0 and 1 when you call random().

Python
from random import random
print(random())

Output
0.8443722499369146

Randomly Select Elements from a List in Python

Random sampling from a list in Python (random.choice, and sample)

Example 1:  Python random.choice() function is used to return a random item from a list, tuple, or string.

Python
import random

a = [1, 2, 3, 4, 5, 6]
print(random.choice(a))

s = "geeks"
print(random.choice(a))

tup = (1, 2, 3, 4, 5)
print(random.choice(tup))

Output
4
k
5

Example 2:  Python random.sample() function is used to return a random item from a list, tuple, or string.

Syntax: random.sample(sequence, length)

It selects three random elements without replacement from a list, a tuple, and a string, demonstrating its versatility in generating distinct random samples. With each execution, the selected elements will differ, providing random subsets from the input data structures.

Python
from random import sample

a = [1, 2, 3, 4, 5]
print(sample(a,3))

b = (4, 5, 6, 7, 8)
print(sample(b,3))

c = "45678"
print(sample(c,3))

Output
[4, 2, 3]
[4, 7, 8]
['6', '4', '8']

Shuffle List in Python

A random.shuffle() method is used to shuffle a sequence (list). Shuffling means changing the position of the elements of the sequence. Here, the shuffling operation is inplace.

Syntax: random.shuffle(sequence, function)

Example: Shuffling a List

The code randomly reorders the elements of list a twice using random.shuffle() and prints the results each time.

Python
import random
a = [1, 2, 3, 4, 5]

random.shuffle(a)
print("After shuffle : ")
print(a)

random.shuffle(a)
print("\nSecond shuffle : ")
print(a)

Output
After shuffle : 
[1, 4, 2, 5, 3]

Second shuffle : 
[1, 4, 3, 5, 2]

In this article we discussed about Python Random module, and also saw some examples of functions in random module in Python. Random module in Python is very important and contains very useful functions.

Hope this helps you in using Python Random module functions.

More on Python Modules:



Article Tags :
Practice Tags :

Similar Reads