Python Random Module

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

Python abs() built-in function Python aiter() built-in function Python all() built-in function Python any() built-in function Python ascii() built-in function Python bin() built-in function Python bool() built-in function Python breakpoint() built-in function Python bytearray() built-in function Python bytes() built-in function Python callable() built-in function Python chr() built-in function Python classmethod() built-in function Python compile() built-in function Python complex() built-in function Python delattr() built-in function Python dict() built-in function Python dir() built-in function Python divmod() built-in function Python enumerate() built-in function Python eval() built-in function Python exec() built-in function Python filter() built-in function Python float() built-in function Python format() built-in function Python frozenset() built-in function Python getattr() built-in function Python globals() built-in function Python hasattr() built-in function Python hash() built-in function Python help() built-in function Python hex() built-in function Python id() built-in function Python __import__() built-in function Python input() built-in function Python int() built-in function Python isinstance() built-in function Python issubclass() built-in function Python iter() built-in function Python len() built-in function Python list() built-in function Python locals() built-in function Python map() built-in function Python max() built-in function Python memoryview() built-in function Python min() built-in function Python next() built-in function Python object() built-in function Python oct() built-in function Python open() built-in function Python ord() built-in function Python pow() built-in function Python print() built-in function Python property() built-in function Python range() built-in function Python repr() built-in function Python reversed() built-in function Python round() built-in function Python set() built-in function Python setattr() built-in function Python slice() built-in function Python sorted() built-in function Python staticmethod() built-in function Python str() built-in function Python sum() built-in function Python super() built-in function Python tuple() built-in function Python type() built-in function Python vars() built-in function Python zip() built-in function