You are currently viewing How to Generate Random Numbers in Python?

You can generate a single random number or multiple random numbers in python by using the random module. It provides several functions to generate random numbers. the random() method is one of the most common methods. It can generate a random float number between 0 and 1 (not including 1). The randint(a, b) function is another function to generate a random integer between a given range of a, b.

Advertisements

Related: Random String Generation with Letters and Digits in Python

In this article, I will explain different ways to generate random numbers using some of the most common functions of the random module of Python with examples.

1. Quick Examples of Generating Random Numbers

Following are quick examples of generating single or multiple random numbers in Python.


# Quick examples of generate random numbers
import random

# Example 1: Generate float random numbers 
random_value = random.random()

# Example 2: Generate random integer using randint()
random_value= random.randint(20,50)

# Example 3: Generate random number Using choice()
random_value = random.choice(marks)
print("Random number:", random_value)

# Example 4: Generate random number 
# using random.randrange()
random_value = random.randrange(10, 20, 2)
print("Random number:", random_value)


# Example 5: Generate float random number Using uniform()
random_value = random.uniform(10,20)
print("Float random number:\n", random_value)

# Example 6:Generate random list using random.sample()
marks = [90,89,78,98,99]
print("List:", marks)
random_list = random.sample(marks, k=3)
print("Random list:", random_list )

2. Generate Random Number using random() in Python

The random.random() function is available in Python from the random module which generates a random float number between 0 and 1. It won’t take any parameter and return a random float number between 0 to 1 which is different for every execution. Before going to generate a random number we need to import a random module.

Following is the syntax of random.random().


# Syntax of random.random()
random.random()

Let’s generate a random number using the random.random() method.


import random
# Generate float random numbers 
random_value = random.random()
print("Float random number:\n", random_value)

Yields below output.

Generate random number in Python

0.09762346702671432 is the float random number that returned by the random.random() method.

3. Generate Random Number using randint()

Python also provides random.randint() from the random module, which is used to generate the random integer from the given range of integers. It takes numeric values as its parameters. These will define the range of integers. So that a random integer is generated within the specified range.

  • Note 1: If we specify parameters other than the numeric values, TypeError will be returned.
  • Note 2: If we specify the float values as its parameters, it will return the ValueError .

Following is syntax of random.randint().


# Syntax of random.randint()
random.randint(start, end)

It takes two parameters.

  • start : It specifies the starting point of the range.
  • stop : It specifies the ending point of the range.

Let’s Pass a specific range of integers(20, 50) into random.randint(), it will generate a random integer within a specific range.


# Import random
import random

# Generate random integer using randint()
random_value= random.randint(20,50)
print("Random integer:", random_value)

Yields below output.

Generate random number in Python

4. Generate Random Number using choice() in Python

In Python random.choice() is another in-built function of the random module, which is used to generate a random number from the given iterable like a list, tuple, or string. It takes an iterable as a parameter and returns a random number from given iterable. 

Following is the syntax of random.choice().


# Syntax of random.choice
random.choice(iterable)

It takes an iterable like a list, tuple, or string as its parameter.

Let’s create a list of numbers and pass it into random.choice() function. It will generate the random number from the list.


import random

# Initialize the list
marks = [78,90,76,89,97,67]
print("List:", marks)

# Generate random number Using choice()
random_value = random.choice(marks)
print("Random number:", random_value)

# Output:
# List: [78, 90, 76, 89, 97, 67]
# Random number: 97

97 has been returned by the random.choice() method.

5. Generate Random Number using randrange()

The random.randrange() is an in-built function of Pyhton from the random module which, is used to generate a random integer between a specified range. It takes two parameters and returns a random number from a specified range.

Following is the syntax of random.randrange().


# Syntax of random.randrange()
random.randrange(start, stop)

It takes three parameters.

  • start: Is optional and it specifies the starting point of the range. If not specified it will take as ‘0’.
  • stop: It specifies the ending point of the range. This function generate random values up to before ending point.
  • step: Is optional and specifies the step value between the numbers in the range. By default it’s value is ‘1’.

Here’s an example of how to use the random.randrange() function to generate a random integer between 1 and 10 (inclusive):


import random
# Generate random number 
# using random.randrange()
random_value = random.randrange(10, 20, 2)
print("Random number:", random_value)

# Output:
# Random number: 14

6. Generate Random Number using Python uniform()

In Python random.uniform() function is a part of the random module which, is used to generate a floating random number from the specified range of values. It takes two parameters that specify the range (lower limit and upper limit). It will return the random number which is including lower limit and an upper limit.

Following is the syntax of random.uniform().


# Syntax of random.uniform
random.uniform(lowerlimit, upperlimit)

Let’s generate the random float number by passing the specified range(10,20) into random.uniform() function. It will return the float random number which can be either 10 or 20.


import random

# Generate float random number Using uniform()
random_value = random.uniform(10,20)
print("Float random number:\n", random_value)

# Output:
# Float random number:
# 10.176997620038726

7. Generate Random Number List using sample()

Python random.sample() function is available in the random module, which will generate a specified length of the list where random numbers are selected from a given sequence/iterable like list, tuple, set, etc. It will take three parameters. The elements are returned in a list.

Following is the syntax of random.sample() function.


# Syntax of random.sample()
random.sample(sequence/iterable, counts, k)

It takes three parameters.

  • sequence: Is the sequence/iterable in which random numbers are generated from this sequence/iterable.
  • counts: It is optional parameter which will represent the frequency of each element in the specified sequence/iterable. We need to pass the frequencies through a list.
  • k : It is the integer value which will specify the length of the sample.

Let’s take a list and pass it into the sample() function along with the specified length(k). It will return the specified random numbers of the list.


import random
# Generate random list using random.sample()
marks = [90,89,78,98,99]
print("List:", marks)
random_list = random.sample(marks, k=3)
print("Random list:", random_list )

# Output:
# List: [90, 89, 78, 98, 99]
# Random list: [90, 99, 78]

8. Conclusion

In this article, I have explained some of the most common functions of the random module of Python and using these syntaxes and parameters how we can generate random numbers/numbers with well-defined examples.

Happy Learning!!

References