Python random module

The random module in Python is used to form random numbers. Here, we will see how to import a random module in a Python program. We will also work on some built-in functions of the random module.

How to import random module in Python

To import the random module and use its functions, write the following at the start of the Python program:

Examples – random module functions

Let us see the following examples of the random module:

  1. random()
  2. randint()
  3. choice()
  4. randrange()

random() function in Python

To return a random float number between 0 & 1, use the random() function in Python. Let us see an example:

The output is as follows:

randint() function in Python

To  return a random integer number between a range, use the randit() function in Python. Let us see an example:

The output is as follows:

choice() function in Python

To return a random element from a sequence, use the choice() function in Python. Let us see an example:

The output is as follows:

randrange() function in Python

To return a random number between a range, use the randrange() function in Python. Let us see an example:

The output is as follows:

Python Tutorial (English)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python math module
Python Programming Examples
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading