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.

Advertisement

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.

Share.
Leave A Reply


Exit mobile version