App: Random Module#
The random module allows for the selection of random numbers or elements from a distribution or sequence.
First, you need to import the random module.
import random
Methods#
You can access the list of methods in the random module by using the dir(random) function.
You can execute help(random) for more details.
print(dir(random))
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_ONE', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_floor', '_index', '_inst', '_isfinite', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randbytes', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
choice()#
It returns a randomly chosen element from a non-empty sequence. The syntax is random.choice(sequence).
If the sequence is empty, it raises an error.
rand_element = random.choice(['a', 'b', 'c', 'd'])
print(rand_element)
b
choices()#
It returna a list of randomly chosen k elements chosen from a sequence with replacement. The syntax is random.choice(sequence).
If the sequence is empty, it raises an error.
rand_list = random.choices(['a', 'b', 'c', 'd'], k=3)
print(rand_list)
['c', 'a', 'a']
randint()#
It returns an integer in a given range. The syntax is random.randint(a, b).
The number is chosen between a and b.
rand_int = random.randint(3,8)
print(rand_int)
7
random()#
It returns a float (decimal) number between 0 and 1. The syntax is random.random()
rand_float = random.random()
print(rand_float)
0.4449043632088713
randrange()#
It returns a random integer within the specified range. The syntax is:
random.randrange(stop): Produces a random integer between 0 and stop-1.
random.randrange(start, stop[, step]): Generates a random integer between start and stop-1 with the specified step size.
It is equivalent to random.choice(range(start, stop, step)).
The following code returns an integer between 0 and 19.
rand_int = random.randrange(20)
print(rand_int)
2
The following code returns an integer from the set: 1, 4, 7, 10, 13, 16, 19.
rand_int = random.randrange(1,20,3)
print(rand_int)
1
uniform()#
It returns a float (decimal) number in a given range. The syntax is random.uniform(a, b).
The number is chosen between a and b.
The following code returns a float number between 5 and 8.
rand_float = random.uniform(5,8)
print(rand_float)
7.645883789778377
seed()#
It fixes the random number generator. The syntax is random.seed(integer).
Fixing the seed ensures that the same random numbers or elements are chosen whenever the code is executed.
It takes an integer as its parameter.
The value 42 is often chosen for this purpose.
42 is chosen because it is the answer to the Ultimate Question of Life, the Universe, and Everything in Douglas Adams’ book “The Hitchhiker’s Guide to the Galaxy”.
When the code below is run, the output may vary each time it’s executed.
rand_element = random.choice(['a', 'b', 'c', 'd'])
print(rand_element)
b
When the code below is executed, the output will be same each time it’s executed.
random.seed(0)
rand_element = random.choice(['a', 'b', 'c', 'd'])
print(rand_element)
d
sample()#
It returns a sample from a given population.
The syntax is random.sample(population, k) where:
population represents the entire set from which the sample is to be drawn.
k denotes the size of the sample to return.
The function returns a new list containing elements randomly picked from the population.
The sample elements are selected without replacement, meaning each element can only be chosen once, ensuring all elements in the sample are unique.
sample = random.sample(['a', 'b', 'c', 'd'], k=3)
print(sample)
['d', 'a', 'b']
shuffle()#
It shuffles a mutable sequence. The syntax is random.shuffle(sequence).
sequence is modified.
letters = ['a', 'b', 'c', 'd']
random.shuffle(letters)
print(letters)
['a', 'c', 'b', 'd']