Python random number | Generator | Function | Module

In Python Generating a Random Number to easily compare to other languages. For Generating a random number in python you need a random object and randint() function. You can get it from the inbuilt random module, so you have to import a Random module in python application. This tutorial you will learn how to get Python random number and random function with examples.

Python random number example | Generator | Function | Module


The syntax of using randint() function.

This returns a number RN in the inclusive range [start,end, meaning start <= RN <= end, where the start and end points are included in the range.



Start and End – is range between you random number required. It should be an integer(number).



The return value is random between a range passed in randint() function in an integer.


Example Random numbers in python 

Here is an example of a random number generator in python and print the result in the console.

Output: 1


The functions of Python Random module

Here is some function of the random module with an example.

  • Bookkeeping functions
  • Functions for integers
  • Functions for sequences
  • Real-valued distributions
  • Alternative Generator

This is groups name of python random modules, we will some of it and if you want to learn more go to official document mentioned in the link in the last.


random.choice(seq) – It will Return a random element from the non-empty sequence seq(tuple, list etc). If sequnce is empty, raises IndexError.

Output: 3


random.randrange(start, stop[, step]) – With this you can add step to move (skip step) , to not slecting every number of step.

Check this example, step = 2. So start with 0 then 0 + step = 2, 3 + step =5 ..will not select as random number. Check output only print 0, 2 or 4.

Output: 0


random.random()– Return the next random floating point number in the range [0.0, 1.0).

Output: 0.09648925468372704


random.shuffle(x[, random])– Shuffle the sequence x in place.

This is some function of the random module, in the official document have many. This all function you can use to do Random number generator in python. It can be your python interview question, so check this below example and do comment if you have any questions a suggestions.


QA: How to Generate a random number between given range.


Example 1: Python random number between 0 and 1

Output: 0


Example 2: How to generate a Random number between 0 and 10 in Python.

Output: 9


Example 3: How to get a Random number between 0 and 100.

Output: 51


QA: Which and where random number used?

The Random number is used in games, lottery system and some application like select one winner participant.


Reference: (Official website – Visit this page to learn about all the)

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Examples of the Genrating random number in python made in Python 3, so it may change its different from python 2 or upgraded versions.


Warning: Do not use this pseudo-random generator for security purposes applications. For security or cryptographic uses, see the secrets module.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.