What is the random module in Python?

The random module is a built-in module that allow us to generate random elements.

import random

seed()

The seed method is used to initialize the random number generator.

>>> random.seed(1)
>>> random.random()
# 0.13436424411240122

Setting the seed to a number will always return the same random number:

>>> random.seed(1)
>>> random.random()
# 0.13436424411240122

>>> random.seed(1)
>>> random.random()
# 0.13436424411240122

>>> random.seed(1)
>>> random.random()
# 0.13436424411240122

>>> random.seed(1)
>>> random.random()
# 0.13436424411240122

>>> random.seed(1)
>>> random.random()
# 0.13436424411240122

The default value of the seed method is the current system time, that is why we always get a different number:

>>> random.random()
# 0.8474337369372327
>>> random.random()
# 0.763774618976614
>>> random.random()
# 0.2550690257394217

randint()

random.randint(start: int, stop: int)

This method returns a random number between a given start and stop parameters:

>>> random.randint(1, 5)
# 3
>>> random.randint(1, 5)
# 2
>>> random.randint(1, 5)
# 5
>>> random.randint(1, 5)
# 1
>>> random.randint(1, 5)
# 3
>>> random.randint(1, 5)
# 1

choice()

The choice method return a randomly selected element from an iterable, like a list, set or str:

>>> random.choice([1, 2, 3, 4])
# 1
>>> random.choice([1, 2, 3, 4])
# 2
>>> random.choice([1, 2, 3, 4])
# 4
>>> random.choice([1, 2, 3, 4])
# 4

shuffle()

The shuffle method takes in an iterable and shuffle it:

>>> my_list = [1, 2, 3, 4]

>>> random.shuffle(my_list)
>>> my_list
# [1, 4, 3, 2]

>>> random.shuffle(my_list)
>>> my_list
# [2, 4, 3, 1]

>>> random.shuffle(my_list)
>>> my_list
# [4, 2, 3, 1]

sample()

random.sample(iterable, k: int)

sample returns a list with a random selection from an iterable. The number of elements returned is equal to the k parameter:

>>> random.sample([1, 2, 3, 4], 1)
# [3]
>>> random.sample([1, 2, 3, 4], 2)
# [3, 4]
>>> random.sample([1, 2, 3, 4], 3)
# [4, 3, 2]
>>> random.sample([1, 2, 3, 4], 4)
# [1, 2, 4, 3]

random()

The random method returns a random floating point number between 0.0 and 1.0:

>>> random.random()
# 0.4143139993007743
>>> random.random()
# 0.17300740157905092
>>> random.random()
# 0.548798761388153
>>> random.random()
# 0.7030407620656315

uniform()

the uniform method is similar to randint, but return a floating point number:

>>> random.uniform(1, 5)
# 3.697943322009309
>>> random.uniform(1, 5)
# 2.498812082006561
>>> random.uniform(1, 5)
# 2.7558465201782525
>>> random.uniform(1, 5)
# 3.0337059529999273

Subscribe to pythoncheatsheet.org

A two times a month and bullshit free publication, full of interesting, relevant links.

What is random module in NumPy?

The random is a module present in the NumPy library. This module contains the functions which are used for generating random numbers. This module contains some simple random data generation methods, some permutation and distribution functions, and random generator functions.

Is random a module or library?

Using the random library The random module is another library of functions that can extend the basic features of python. Other modules we have seen so far are string, math, time and graphics. With the exception of the graphics module, all of these modules are built into python.

Is Python random module really random?

The random number or data generated by Python's random module is not truly random; it is pseudo-random(it is PRNG), i.e., deterministic. The random module uses the seed value as a base to generate a random number.

What is random used for?

Randomness has many uses in science, art, statistics, cryptography, gaming, gambling, and other fields. For example, random assignment in randomized controlled trials helps scientists to test hypotheses, and random numbers or pseudorandom numbers help video games such as video poker.