Home / Programs / Directly generates a list of random numbers using sample method
🚀 Programming Example

Directly generates a list of random numbers using sample method

👁 239 Views
💻 Practical Program
📘 Step Learning

In this program you will how to generates a list of random numbers directly using sample method without a loop

📌 Information & Algorithm

The random module also provides the sample() method, which directly generates a list of random numbers. Below is the example of generating random numbers using the sample() method.

💻 Program Code

import random  
#Generate 5 random numbers between 10 and 30  
random_list = random.sample(range(10, 40), 6)  
print(random_list)  
                        

🖥 Program Output

[18, 25, 26, 29, 14, 31]
                            

📘 Explanation

In the above code, we have used the range() function, which generates the numbers between the given range.

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