| by Arround The Web | No comments

Random Integer Python

In Python, the Integers are represented as positive or negative whole numbers, including zero. Sometimes in Python, we need to generate random integer numbers for various tasks, such as training AI neural networks or large datasets. To perform this particular task, Python provides various modules, such as “random”.

This write-up will present a comprehensive tutorial on generating random integers in Python using numerous examples.

How to Generate Random Integers in Python?

The following methods are utilized to generate/create a random integer in Python:

Method 1: Generate a Random Integer Using “random.randint()” Method

In Python, the “randint()” method of the “random” module is utilized to retrieve a random int number from a particular range. The syntax of this is shown below:

random.randint(start, stop)

 

Let’s use this method to generate a random integer or number by utilizing the below code:

import random
print(random.randint(10, 30))

 

In the above code, the “random.randint()” takes the “10” as the first argument, which indicates the start of the range, and “30” as a second argument which indicates the end of the range. This method retrieves the random number within the particular range.

Output

Method 2: Generate a Random Integer Using “random.randrange()” Method

The “random.randrange()” method can also be utilized to retrieve the random integer from the specified range. Here is the syntax of this method:

random.randrange(start, stop, step)

 

Example 1: Using Only the Stop Parameter Value

This method is utilized in the following code to generate a random integer:

import random
print(random.randrange(10))

 

Here, the “random.randrange()” method takes only one argument “, 10”, which indicates the end of the range. This method takes the start of the range value “0” by default and returns the random integer within that range.

Output

Example 2: Using the Start and Stop Parameter Value

Here is an example code that explains the working of randrange() function with start and stop parameters:

import random
print(random.randrange(10, 15))

 

In the above code, the “random.randrange()” method takes the start and stop value as an argument and generates the random integer within that specified range.

Output

Example 3: Using the Start, Stop, and Step Parameter Value

Let’s overview the below code:

import random
print(random.randrange(10, 30, 5))

 

In this code block, the “random.randrange()” method generates the random integer within a specified range based on the step value.

Output

ValueError in Python

The “start” value of the range must be less than or equal to the “stop” parameter value; otherwise, the “ValueError” occurs in Python. Here is an example:

import random
print(random.randrange(100, 30,5))

 

The above code generates the following output:

Note: The “start” value can be greater than the “stop” parameter in case of a negative step value.

We can’t pass the “0” value to the “random.randrange()” method as the “step” parameter value. For example, in the below code, the “0” is passed to the “step” parameter:

import random
print(random.randrange(100, 30,0))

 

The above code shows the following output:

That is all about generating random integers in Python.

Conclusion

The “random.randint()” or the “random.randrange()” methods of the “random” module are utilized to generate/create a random integer in Python. These methods are utilized to create/generate the random integers within the particular range. The “randrange()” method generates a random integer value with steps from a specified range. This Python write-up delivered a detailed guide on how to generate random integers using various methods.

Share Button

Source: linuxhint.com

Leave a Reply