Skip to content

How to Generate Random Numbers in Python? Gottumukkala Sravan Kumar Spark By {Examples}

  • by

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.

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 impost a random module.

2.1 Syntax of random.random()

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.

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

3. Generate Random Number using randint()

Python provides random.randint() from random module, which is used to generate the random integer from the given range of integers. It should take 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 the 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 .

3.1 Syntax of random.randint()

Following is syntax of random.randint() .

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

3.2 Parameters

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 specific range of integers(20, 50) into random.randint() it, will generate a random integer with in a specific range.

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

Yields below output.

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. 

4.1 Syntax of random.choice()

Following is the syntax of random.choice().

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

4.2 Parameters

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 and return 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 random number from a specified range.

5.1 Syntax of random.randrange()

Following is the syntax of random.randrange().

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

5.2 Parameters

It takes three parameters.

start: Is optional and it specifies the starting point of the range. If not specifies 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.

6.1 Syntax of random.uniform()

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 specifies the 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.

7.1 Syntax of random.sample()

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

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

7.2 Parameteres

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 number/numbers with well-defined examples.

Happy Learning!!

 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  Read More Python, Python Tutorial 

Leave a Reply

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