Home / Programs / Generate a Random Number within a Given Range
🚀 Programming Example

Generate a Random Number within a Given Range

👁 242 Views
💻 Practical Program
📘 Step Learning

In this program you will learn how to generate a random number within a given range.

📌 Information & Algorithm

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.

💻 Program Code

import random  
n = random.randint(0,50)  
print(n)  
                        

🖥 Program Output

40
                            

📘 Explanation

Another Example

import random  
n = random.randint(100, 200)  
print(n)  

Output

143
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.