Generate a Random Number within a Given Range
In this program you will learn how to generate a random number within a given range.
In this program you will learn how to generate a random number within a given range.
Python random module provides the randint() method that generates an integer number within a specific range. We can pass the two numbers as arguments that defines the range. Let's understand the following example.
import random
n = random.randint(0,50)
print(n)
40
Another Example
import random n = random.randint(100, 200) print(n)
Output
143
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.