The Python random module offers you a variety of functions for gen­er­at­ing random numbers in various formats, ranging from integers to floating-point numbers to selecting elements from lists.

What is Python’s random module and what can it be used for?

The Python random module random is a built-in library that allows you to generate random numbers and perform random-based op­er­a­tions in your programs. It includes various functions for gen­er­at­ing pseudo-random numbers that can be used in many ap­plic­a­tions, from sim­u­la­tion to game de­vel­op­ment and cryp­to­graph­ic tasks.

An important char­ac­ter­ist­ic of the random module is its ability to provide re­pro­du­cible results. By es­tab­lish­ing a starting value or seed, the process of random gen­er­a­tion can be re­pro­duced. This is be­ne­fi­cial for testing, ex­per­i­ments and sim­u­la­tions that require con­sist­ent random data.

What are the functions in the random module?

The Python random module contains various methods for gen­er­at­ing and pro­cessing random numbers. The following table provides an overview of the functions and their prop­er­ties:

Name of function Ex­plan­a­tion
seed(a=None, version=2) Defines the initial value (seed) for the random number generator
getstate() Returns the current state of the random number generator as an object
setstate(state) Resets the state of the random number generator using a state object
getrandbits(k) Returns an integer with k bits
randrange(start, stop, step) Creates a random integer from the specified range
randint(a, b) Returns a random integer in the specified range
choice(seq) Returns a random element from the given sequence
choices(population, weights=None, *, cum_weights=None, k=1) Creates a list with k randomly selected elements from the pop­u­la­tion; optional to specify the prob­ab­il­it­ies
sample(k, population) Creates a list with k randomly selected elements from the pop­u­la­tion, without du­plic­ates
shuffle(x) Shuffles the elements in a list in random order
random() Returns a random floating-point number between 0 and 1
uniform(a, b) Returns a random floating-point number in the specified range, including the limit values
triangular(low, high, mode) Creates a random floating-point number in the tri­an­gu­lar dis­tri­bu­tion range
betavariate(alpha, beta) Returns a random floating-point number from a beta dis­tri­bu­tion
expovariate(lambd) Returns a random floating-point number from an ex­po­nen­tial dis­tri­bu­tion
gammavariate(alpha, beta) Creates a random floating-point number from a gamma dis­tri­bu­tion
gauss(mu, sigma) Returns a random floating-point number from a Gaussian dis­tri­bu­tion
lognormvariate(mu, sigma) Creates a random floating-point number from a log­ar­ithmic normal dis­tri­bu­tion
normalvariate(mu, sigma) Returns a random floating-point number from a normal dis­tri­bu­tion
vonmisesvariate(mu, kappa) Returns a random floating-point number with a von Mises dis­tri­bu­tion or circular normal dis­tri­bu­tion
paretovariate(alpha) Returns a random floating-point number with a Pareto dis­tri­bu­tion
weibullvariate(alpha, beta) Returns a random floating-point number with a Weibull dis­tri­bu­tion

How to select elements randomly from a list

If you want to select several random elements from a list, you can use the function choices(seq, k=n) from the Python random module, where n is the number of desired random elements.

import random
# A list of elements
my_list = ['Apple', 'Banana', 'Orange', 'Strawberry', 'Cherry']
# Randomly selecting an element from the list
random_elements = random.choices(my_list, k=3)
print("Randomly selected elements:", random_elements)
python

In this case, we use the choices() function to select three random elements from my_list. The results are returned as a list of three random elements.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

How to shuffle a list

The shuffle() function arranges Python list elements in a random order.

import random
# A list of elements
my_list = ['Apple', 'Banana', 'Orange', 'Strawberry', 'Cherry']
# Shuffle the elements in the list
random.shuffle(my_list)
print("Shuffled list:", my_list) # Example Output: Shuffled list: ['Strawberry', 'Banana', 'Apple', 'Cherry', 'Orange']
python

You should keep in mind that shuffle() changes the list itself and does not return a new one. After using shuffle(), the original list will be in a random order.

How to set a seed value

When you use random.seed() from the Python random module to set a specific seed value, it ini­tial­ises the random number generator to produce random numbers using this seed. Therefore, if you set the same seed value again later, it will result in the identical sequence of random numbers being generated in your ap­plic­a­tion.

import random
# Setting the seed value to 42
random.seed(42)
# Generating random numbers
print(random.random())  # Example output: 0.6394267984578837
print(random.random())  # Example output: 0.025010755222666936
# Resetting the seed value to 42
random.seed(42)
# Generating random numbers again
print(random.random())  # Example output: 0.6394267984578837 (identical to the previous value)
print(random.random())  # Example output: 0.025010755222666936 (identical to the previous value)
python

After the seed value is set, it generates and outputs random numbers. Resetting the seed to the same value results in the same sequence of random numbers being produced again. In this example, the seed value is 42. As long as this seed remains constant, the random numbers can be con­sist­ently rep­lic­ated.

Go to Main Menu