Generate pseudo-random numbers in Python


Many computer applications need random number to be generated. However, none of them generate a truly random number. Python, like any other programming technique, uses a pseudo-random generator. Python’s random generation is based upon Mersenne Twister algorithm that produces 53-bit precision floats. The technique is fast and thread-safe but not suitable from cryptographic purpose.

Python’s standard library contains random module which defines various functions for handling randomization.

random.seed() − This function initializes the random number generator. When random module is imported, the generator is initialized with the help of system time. To reseed the generator, use any int, str, byte or bytearray object.

random.getstate() − This function along with setstate() function helps in reproducing same random data again and again. The getstate() function returns internal state of random number generator.

random.setstate() − This function reinstates the internal state of generator.

Following functions handle random integer number generation −

random.randrange() − This function generates a random integer between given range. It can take three parameters.

random.randrange(start, stop, step)

The start and step parameters are optional. Their default values are 0 and 1 respectively. Step determines interval between successive numbers.

>>> random.randrange(10)
5
>>> random.randrange(10, 20)
17
>>> random.randrange(100, 200, 2)
188

(Note: Remember that output of above statements, and also rest of the statements in this article may not be same as they are randomly generated)

random.randint() − This function generates a random integer between two parameters. It is similar to randrange() function without step parameter. Start parameter is mandatory.

>>> random.randint(1,10)
6
>>> random.randint(100, 200)
134

Following functions deal with floating point random numbers.

random.random() − This function randomly generates a floating point number between 0.0 and 1.0

>>> random.random()
0.668544544081956

random.uniform() − This function returns a floating point random number between two parameters.

>>> random.uniform(0.5,1.5)
1.2760281470664903
>>> random.uniform(1,10)
7.336985794193224
>>> random.uniform(10,5)
7.817794757786727

Following functions act upon sequence objects viz. string, list or tuple.

random.choice() − This function picks a random element from the sequence. If the sequence is empty, IndexError is thrown.

>>> random.choice("Tutorialspoint")
'o'
>>> random.choice(range(10))
2
>>> random.choice([15,31,6,29,55, 5])
55
>>> random.choice((15,31,6,29,25, 55))
15

random.choices() − This function chooses multiple elements from a list in random manner. First parameter to this function is the sequence and second parameter is the number of choices to be made.

>>> name = "TutorialsPoint"
>>> random.choices(name, k = 2)
['T', 'o']

random.shuffle() − This function reorders elements in a mutable sequence and places them randomly.

>>> num = [10,20,30,40,50]
>>> random.shuffle(num)
>>> num
[50, 20, 40, 30, 10]

random.sample() − This function works with immutable sequence. It returns a list of randomly selected items from the sequence leaving it intact.

>>> name = "TutorialsPoint"
>>> nm = random.sample(name, k = 2)
>>> name, nm
('TutorialsPoint', ['i', 'a'])

Updated on: 27-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements