Home / Programs / Python Program to Generate a Random Number
🚀 Programming Example

Python Program to Generate a Random Number

👁 256 Views
💻 Practical Program
📘 Step Learning

In this program you will learn how to generate a random number using python program.

📌 Information & Algorithm

In Python programming, you can generate a random integer, doubles, longs etc . in various ranges by importing a "random" class.

In Python, we can generate a random integer, doubles, long, etc in various ranges by importing a "random" module. In the following example, we will learn how to generate random numbers using the random module.

Syntax:

First, we have to import the random module and then apply the syntax:

 
import random  

Generating a Random Number

The random module provides a random() method which generates a float number between 0 and 1. Let's see the following example.

💻 Program Code

import random  
n = random.random()  
print(n)  
                        

🖥 Program Output

 
0.7632870997556201
 
 
If we run the code again, we will get the different output as follows. 
 
0.8053503984689108
 
                            

📘 Explanation

na
📚 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.