Skip to content

What are the Python Random Functions?

Category
Python
Versions & support
About Python →
On this page

Python provide multiple functions to generate random numbers. Most of these functions are available under the Python random module. You can use these functions for generating random numbers in your Python scripts.

Here is the five main functions used for generating random numbers in a Python script.

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

1. Python random() function:

This function returns a random floating point value between 0 to 1. You can also specify the range to override the defaults.

2. Python randint() function:

The randint() function takes in a range and produces an integer number between the defined range.

3. Python uniform() function:

Just as the randint() function generates an integer within a given range, uniform() does the same for floating point numbers.

4. Python randrange() function:

The randrange() function is used to select a integer value between a defined range. You can also specify to select even or odd number between a range.

5. Python choice() function:

Python choice() function is used to select a single random element from a sequence.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top