What are the Python Random Functions?

Channel: Linux
Abstract: 2) print (x)12345678910111213import random # Print a Integer number between 0 to 9x = random.randrange(10)print (x) # Print a integer number between 1

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.

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)12345678910111213import random # Print a random float number between 0 and 1x = random.random()print (x) # Print a random float number between 0 and 10x = random.random() * 10print (x) # Print a random float number between -5 and +5x = random.random() * 10 - 5print (x)

2. Python randint() function:

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

import random # Print a random integer between 10 and 100 x = random.randint(10, 100) print (x)12345import random # Print a random integer between 10 and 100x = 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.

import random # Print a floating point number between 10 and 50 x = random.uniform(10, 50) print (x)12345import random # Print a floating point number between 10 and 50x = 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.

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)12345678910111213import random # Print a Integer number between 0 to 9x = random.randrange(10)print (x) # Print a integer number between 10 to 99x = random.randrange(10, 100)print (x) # Print a Even integer number between 10 to 99x = 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.

import random # Select a random element from below sequence x = random.choice(['red', 'geen', 'yellow']) print (x)12345import random # Select a random element from below sequencex = random.choice(['red', 'geen', 'yellow'])print (x)

Ref From: tecadmin
Channels: Pythonfunctions

Related articles