Sometimes, in making programs for gaming or gambling, we come across the task of creating the list all with random numbers. This task is to perform in general using loop and appending the random numbers one by one. But there is always a requirement to perform this in most concise manner. Let’s discuss certain ways in which this can be done.
Method #1 : Using list comprehension +
The naive method to perform this particular task can be shortened using the list comprehension. randrange function is used to perform the task of generating the random numbers.
Random number list is : [21, 27, 23, 20, 21, 25, 15]
Method #2 : Using
This single utility function performs the exact required as asked by the problem statement, it generated N no. of random numbers in a list in the specified range and returns the required list.
Random number list is : [8, 46, 45, 49, 15, 6, 31]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Generating random Id's in Python
- Generating Random id's using UUID in Python
- Generating random strings until a given string is generated
- Generating Random Integers in Pandas Dataframe
- random.random() function in Python
- Random sampling in numpy | random() function
- Reading and Generating QR codes in Python using QRtools
- Generating hash id's using uuid3() and uuid5() in Python
- Generating Word Cloud in Python
- Generating Word Cloud in Python | Set 2
- Generating Strong Password using Python
- Python - Generate random number except K in list
- Python | Generate random number except K in list
- Generating subarrays using recursion
- Generating Basic Discrete Time Signals
- Generating all possible Subsequences using Recursion
- Python | Generate random numbers within a given range and store in a list
- Python | Select random value from a list
- Python | Remove random element from list
- Python - Get a sorted list of random integers with unique elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.