Home / Programs / Generate a list of random numbers in python
🚀 Programming Example

Generate a list of random numbers in python

👁 265 Views
💻 Practical Program
📘 Step Learning
In this python program we will learn how to generated a list of random numbers in python.

📌 Information & Algorithm

The randint() method can be used with for loop to generated a list of random numbers. To do so, we need to create an empty list, and then append the random numbers generated to the empty list one by one. Let's understand the following example.

💻 Program Code

import random  
rand_list = []  
for i in range(0,10):  
    n = random.randint(1,50)  
    rand_list.append(n)  
print(rand_list)  
                        

🖥 Program Output

[10, 49, 16, 31, 45, 21, 19, 32, 30, 16]
                            

📘 Explanation

Using randint() method with for loop you can generate a list of random numbers.

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