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.

- random()
- randint()
- uniform()
- randrange()
- 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 | import random # Print a random float number between 0 and 1 x = random.random() print (x) # Print a random float number between 0 and 10 x = random.random() * 10 print (x) # Print a random float number between -5 and +5 x = random.random() * 10 - 5 print (x) |

## 2. Python randint() function:

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

1 2 3 4 5 | import random # Print a random integer between 10 and 100 x = random.randint(10, 100) print (x) |

## 3. Python uniform() function:

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

1 2 3 4 5 | import random # Print a floating point number between 10 and 50 x = random.uniform(10, 50) print (x) |

## 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 | import random # Print a Integer number between 0 to 9 x = random.randrange(10) print (x) # Print a integer number between 10 to 99 x = random.randrange(10, 100) print (x) # Print a Even integer number between 10 to 99 x = random.randrange(10, 100, 2) print (x) |

## 5. Python choice() function:

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

1 2 3 4 5 | import random # Select a random element from below sequence x = random.choice(['red', 'geen', 'yellow']) print (x) |

## Leave a Reply