Create a List of Random Numbers — The Most Pythonic Way

Do you want to initialize a list with some random numbers? In this article, I’ll show you four different way of accomplishing this—along with a short discussion about “the most Pythonic way”.

Problem: Given an integer n. Create a list of n elements in a certain interval (example interval: [0, 20]).

# n = 5 –> [2, 3, 1, 4, 3]
# n = 3 –> [10, 12, 1]
# n = 10 –> [8, 2, 18, 10, 4, 19, 5, 9, 8, 1]

Solution: Here’s a quick overview on how you can create a list of random numbers:

Method 1: [random.random() for _

Continue reading the original article