Examples
Randint with Positive and Negative integrals
Code in Python
import random
print(“Random value between 1 and 100 :”, random.randint(1,100))
print(“Random value between -20 and 0 :”,random.randint(-20,0))
Output
Random value between 1 and 100 : 70
Random value between -20 and 0 : -3
Randint for Decimal or Floating Point values
Code in Python
import random
print("Random value between 23.5 and 68.7 :",random.randint(23.5,68.7))
Output

Randint for Non-numeric Values like String or Character Values
Code in Python
import random
print("Random value between a and b :",random.randint('a','b'))
Output

Applications
The randint function finds its application in many fields, such as gaming, simulations, and cryptography.
-
In gaming, the randint function simulates dice rolls, coin tosses, and other random events.
-
In simulations, the randint function can generate random values for physical system variables like position, velocity, and acceleration.
-
In cryptography, the randint function generates random keys and nonces. Cryptography heavily relies on randomness, and the ability to generate truly random values is essential in ensuring cryptographic systems' security.
Here is a lottery system in which we use the randint() function.
Code in Python: Lottery System
#importing random library for using randint
import random
# Define the range of numbers for the lottery
MIN_NUMBER = 1
MAX_NUMBER = 50
# Define the number of lottery balls to draw
NUM_BALLS = 6
#We will keep adding the randomly generated number to the list created below
winning_numbers = []
#the loop will run the name number of times are the number of balls
while len(winning_numbers) < NUM_BALLS:
#generating the random winning number
number = random.randint(MIN_NUMBER, MAX_NUMBER)
#check if the number is already in the list
if number not in winning_numbers:
#add the number to the list if it is not already present
winning_numbers.append(number)
# Sort the winning numbers in ascending order
winning_numbers.sort()
# Print the winning numbers
print("The winning numbers are:")
for number in winning_numbers:
print(number)
Output
The winning numbers are:
14
18
24
27
28
49
This code generates a set of six random numbers between 1 and 50, sorts them in ascending order, and prints them out as the winning numbers for the lottery.
You can modify the values of MIN_NUMBER, MAX_NUMBER, and NUM_BALLS to change the range of numbers and the number of balls drawn for your specific lottery game.
Also see, Convert String to List Python
Frequently Asked Questions
What is the range of values that the randint function can generate?
The range of values that the randint function can generate includes both the lower and upper bounds. For example, if the lower bound is 1 and the upper bound is 10, the randint function can generate any integer between 1 and 10, including 1 and 10.
Can randint generate floating-point numbers?
No, randint can only generate integers and not floating-point numbers. If you need to generate random floating-point numbers, you can use the random.random function instead of the randint function.
How can I generate a random integer in Python without using randint?
You can use the randrange function from the random module to generate a random integer within a range. For example, random.randrange(1, 10) generates a random integer between 1 and 9.
Conclusion
The randint function in Python is a powerful tool for generating random integers between two specified values. It is an essential function in the random module and is frequently used in various applications, such as gaming, simulations, and cryptography.
Check out the following blogs:
Check out some amazing Guided Paths on topics such as Basics of Python, Oops in Python, Basics of C, etc., along with some Contests and Interview Experiences only on Coding Ninjas Studio.
Do check out The Interview Guide for Product Based Companies, as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc., on Coding Ninjas Studio.
Happy Learning!!