29 July 2020   |   by Jatin Jeena   |   Python, Blogs

Python Random Module

Important functions of Python's Random Module with examples


If you are confused in using the random module of Python, then your problem is solved. This article contains the most important functions of the python random module, along with examples and code.




Python provides random module to generate random numbers.

You just need to import random module before starting, just write the line “import random” at the top.




Few important functions of the module are:

1. random()

This function is used to generate a random number between 0 and 1. It return a float value.

2. randint(a, b)

This function returns a random integer between specified range. Both starting value(a in given example) and ending value(b in given example) are inclusive.

3. choice()

This function returns a randomly selected element from a non-empty sequence.

4. shuffle()

his function randomly shuffles, reorders the element of the sequence.

5. randrange(beg, end, step)

This function is used to generate a number between a specified range. Range is specified in the argument of the function.

It takes 3 arguments

Beg – beginning of the range

End- ending of the range

Step – the number which you want to remove from the range, i.e. the number which you want to avoid from selection, is kept as third parameter.

6. uniform(a, b)

This function is used to generate a floating point random number between the numbers mentioned in its arguments.