5 Best Ways to Generate Random Numbers from 1 to 20 in Python and Append Them to a List

πŸ’‘ Problem Formulation: You need a Python program that can generate a series of random numbers within the range of 1 to 20 and then append these numbers to a list for further processing. For instance, you might want an input which is the capacity of the list (e.g. 10 numbers) and the output to be a list – for example, [12, 3, 17, 5, 1, 20, 4, 16, 8, 10].

Method 1: Using the random.randint() Function

Utilizing Python’s random.randint(a, b) function is the most straightforward method to generate random integers between a and b, inclusive. This approach works by calling the function multiple times inside a loop and appending each generated number to a list.

Here’s an example:

import random

numbers_list = []

for _ in range(10):
    numbers_list.append(random.randint(1, 20))

Output:

[16, 8, 1, 10, 20, 14, 2, 11, 19, 17]

This code snippet generates 10 random integers within the 1-20 range. For each iteration of the for loop, the random.randint(1, 20) function is called to generate a number which is then appended to the numbers_list using the append() method.

Method 2: Using a List Comprehension with random.randint()

A more Pythonic way of filling a list with random numbers is to use a list comprehension, which provides a more compact and readable alternative. A list comprehension encapsulates the process of iterating through a sequence, generating random numbers, and appending them to a list into one line of code.

Here’s an example:

import random

numbers_list = [random.randint(1, 20) for _ in range(10)]

Output:

[12, 5, 7, 19, 3, 16, 8, 10, 2, 14]

This snippet uses a list comprehension to create a list of 10 random numbers. For each _ in the range of 10, random.randint(1, 20) is called to generate a random number that’s immediately placed into the numbers_list.

Method 3: Using the random.choices() Function

The random.choices() function, introduced in Python 3.6, is designed to generate a list of random numbers with a specified size, which makes it perfect for our use case without needing a loop.

Here’s an example:

import random

numbers_list = random.choices(range(1, 21), k=10)

Output:

[1, 20, 15, 3, 12, 6, 7, 17, 18, 8]

In this code, random.choices() takes two arguments: a sequence to choose from (in this case, range(1, 21)) and the number of elements to pick (k=10). It returns a new list with the selected random elements.

Method 4: Using random.sample() for Unique Numbers

When a list of unique random numbers is needed, the random.sample() function can be used. It ensures that there are no duplicates among the selected random numbers. However, it should be noted that this method will not work if the sample size is greater than the population size.

Here’s an example:

import random

numbers_list = random.sample(range(1, 21), 10)

Output:

[11, 1, 16, 2, 18, 7, 5, 3, 9, 20]

This snippet uses random.sample(population, k) where the population is the range from 1 to 20, and k is the desired sample size. The returning list numbers_list will contain a unique set of 10 numbers from the range.

Bonus One-Liner Method 5: Using NumPy

If you’re working with numerical data, NumPy library can be highly efficient for generating random numbers. Its numpy.random.randint() function can be used to create an array of random integers, which can then be converted to a list.

Here’s an example:

import numpy as np

numbers_list = list(np.random.randint(1, 21, size=10))

Output:

[14, 2, 6, 17, 8, 13, 5, 19, 3, 20]

This snippet uses NumPy’s np.random.randint(low, high, size) to generate an array of random numbers with the specified size. The result is then cast to a list with list().

Summary/Discussion

  • Method 1: random.randint(): Straightforward and easy to understand. Less Pythonic than using list comprehensions. Good for beginners.
  • Method 2: List Comprehension with random.randint(): Compact and readable. Pythonic approach to generating lists. Equally as random as the loop method.
  • Method 3: random.choices(): Convenient for generating lists of random numbers with potential duplicates. Does not handle unique number generation.
  • Method 4: random.sample(): Best when unique elements are required. Cannot be used when sample size is greater than the set of unique numbers.
  • Method 5: Using NumPy: Highly efficient for numerical computations. Requires an external library (NumPy). Overkill for simple tasks where pure Python suffices.