Python provides multiple in-built and open-source modules and functions to perform various tasks. For instance, to create random numbers, strings, passwords, etc. the “random” module is used in Python. To generate the random integers within a specified range the “randrange()” and “randint()” functions are used in Python.
This post demonstrates the usage of the Python “randrange()” and “randint()” functions with examples.
How to Generate Random Integer Within a Range Using randint() Function?
The “random.randint()” function is used to generate the random integer within the specified range in Python. The syntax of the “random.randint()” function is displayed below:
random.randint(start, stop)
The “start” and “stop” parameters indicate the start and stop position/range of a random integer.
Example 1: Random Positive Integer
The below code generates the random positive integer:
Code:
import random
output = random.randint(0, 999)
print(output)
The “random.randint()” function accepts “0” and “999” as the start and stop parameters. As a result, the stated function generates a random int number within a particular range.
Output:
The random integer “132” has been generated from the given integer range.
Example 2: Random Negative Integer
The below code generates the random negative integer within the given range:
Code:
import random
output = random.randint(-50, -10)
print(output)
The “random.randint()” function accepts the negative starting and ending positions as arguments. Consequently, it generates a random negative integer within the specified range.
Output:
The above output shows that a random integer “-41” has been generated.
Example 3: Random Integer List
In the below code, the random integer list is created using the “random.randint()” function:
Code:
import random
list_random = []
for i in range(0, 5):
rdg = random.randint(0, 100)
list_random.append(rdg)
print(list_random)
In the above code:
- The empty list named “list_random” is created in the program.
- To iterate over the range/length of the list, the “for” loop is used in a program.
- The “randint()” function is used to retrieve the random integer numbers within a particular range.
- The “append()” function will append the random numbers into an empty list until the maximum range. (as specified in for loop).
Output:
The above snippet shows the random integer list.
How to Generate Random Integers Within a Range Using randrange() Function?
The “randrange()” is a built-in function used to generate the random integers within the specified range. The syntax of the “randrange()” function is shown below:
random.randrange(start, stop, step)
The “start”, “stop” and “step” parameters indicate the starting, ending, and equally spaced step size position of the random integer.
Here’s an example:
Example 1: Random Positive Integer
The below code is used to create the random positive integer using the “randrange()” function:
Code:
import random
output = random.randrange(10, 50)
print(output)
The “randrange()” function takes “10” and “50” as arguments that indicate the starting and ending range. The random number will be generated within the specified range.
Output:
The above snippet displays the random positive integer.
Example 1: Random Integer With Multiple of n
The below code is used to generate the random integer with multiple of n:
Code:
import random
output = random.randrange(0, 50, 6)
print(output)
The “randrange()” function takes the starting, stopping, and step values as an argument. The step value indicates that the random integer returned will be multiple of “6”.
Output:
The above snippet shows the random integer value generated within the specified range and is a multiple of 6.
Example 3: Random Negative Integer
The below code generates the random negative integer value via the random.randrange() function:
Code:
import random
output = random.randrange(-50, -10)
print(output)
The “random.randrange()” function takes the negative values as arguments and returns a negative random integer.
Output:
The above output shows the random negative integer value.
Conclusion
The “randrange()” and “randint()” functions are used to generate the random integers within the specified range in Python. The “randint()” function takes the starting and ending range as an argument and returns a random integer within the specified range. The “randrange()” function can be utilized to return a random integer with a multiple of n. This Python guide explained the “randrange()” and “randint()” functions with numerous examples.