Python Generate N Random Numbers: 4 Easy Ways

How-to basics · Updated November 23, 2026

To generate n random numbers in Python, the simplest way is to use a list comprehension with the random module: [random.random() for _ in range(n)] for floats, or [random.randint(a, b) for _ in range(n)] for integers. For reproducible results, set the seed with random.seed(42) before generating. This article covers the four most common methods, how to choose between them, and common pitfalls.

Why Use the random Module?

Python’s built-in random module is the go‑to for most non‑cryptographic randomness. It implements the Mersenne Twister pseudo‑random number generator, which is fast and has a very long period (219937−1). It’s suitable for simulations, games, sampling, and testing. However, it is not suitable for security‑sensitive tasks like password generation or cryptographic keys—for those, use the secrets module instead.

Method 1: List Comprehension with random.random()

If you need n floating‑point numbers between 0.0 (inclusive) and 1.0 (exclusive), use random.random():

import random
n = 5
floats = [random.random() for _ in range(n)]
print(floats)  # e.g., [0.3745401188473625, 0.9507143064099162, ...]

For floats in a different range, say between low and high, use random.uniform(low, high):

floats_in_range = [random.uniform(10, 20) for _ in range(n)]

Method 2: Generating Integers with randint()

To get n integers between two inclusive bounds, use random.randint(a, b):

import random
n = 4
ints = [random.randint(1, 10) for _ in range(n)]
print(ints)  # e.g., [7, 3, 8, 1]

Note that randint(a, b) includes both endpoints. If you prefer an exclusive upper bound, use random.randrange(start, stop) which behaves like range().

Method 3: Using random.choices() for Weighted or Repeated Values

If you need n values drawn from a fixed population (with or without weights, and allowing repeats), use random.choices(). It returns a list of k items:

import random
population = ['red', 'green', 'blue']
choices = random.choices(population, k=5)
print(choices)  # e.g., ['blue', 'red', 'blue', 'green', 'blue']

You can also supply weights:

weighted = random.choices(population, weights=[1, 2, 3], k=5)

Method 4: Unique Numbers with random.sample()

When you need n distinct values from a range or list, use random.sample(population, k). It returns a new list of unique elements without replacement:

import random
unique_ints = random.sample(range(100), 5)  # 5 unique integers from 0-99
print(unique_ints)  # e.g., [17, 82, 44, 3, 91]

If k exceeds the population size, it raises a ValueError. For large populations, random.sample is efficient because it uses a partial Fisher‑Yates shuffle.

How to Choose the Right Method

  • Floats in [0,1) → random.random()
  • Floats in any range → random.uniform(a, b)
  • Integers inclusive → random.randint(a, b)
  • Integers exclusive upper bound → random.randrange(a, b)
  • Repeated picks from a list → random.choices(population, k=n)
  • Unique picks from a list → random.sample(population, k=n)

Reproducibility and Seeding

To make your random numbers reproducible (useful for debugging or sharing results), set the seed once at the start of your script:

import random
random.seed(42)  # any integer works
print([random.random() for _ in range(3)])  # always the same three numbers

Without a seed, Python seeds from the system time, so results differ each run.

Common Pitfalls and Misconceptions

  • Misconception: random.random() returns numbers from 0 to 1 inclusive. It returns 0.0 ≤ x < 1.0. The upper bound is exclusive.
  • Pitfall: Using random.sample with k larger than the population. This throws a ValueError. Check your k.
  • Pitfall: Forgetting to import random. The module is not imported by default.
  • Misconception: random is secure. It is not. Use secrets for passwords or tokens.

Frequently Asked Questions

How do I generate n random numbers in a specific range?

Use [random.randint(low, high) for _ in range(n)] for integers, or [random.uniform(low, high) for _ in range(n)] for floats. Both include the lower bound; randint includes the upper bound, while uniform includes the upper bound as well (it can return high in rare cases).

Can I generate n random numbers without duplicates?

Yes, use random.sample(population, n) where population is a range or list. For example, random.sample(range(100), 10) gives 10 unique integers from 0 to 99. If you need floats without duplicates, generate a list and shuffle it, or use a set until you have n unique values.

What's the difference between random.randint and random.randrange?

randint(a, b) includes both endpoints, so it can return b. randrange(a, b) excludes b, like Python's range(). For example, randrange(1, 7) gives values 1–6, which is useful for dice.

How do I generate random numbers from a normal distribution?

Use random.gauss(mu, sigma) for a single value, or [random.gauss(0, 1) for _ in range(n)] for n values. For other distributions, see the random module's documentation (e.g., expovariate, betavariate).

Is there a faster way to generate many random numbers?

For large n, list comprehensions are fine, but NumPy is significantly faster. With NumPy, use numpy.random.rand(n) for floats, numpy.random.randint(low, high, size=n) for integers, and numpy.random.choice for sampling. This is especially true for n > 10,000.

Alternatives: NumPy and secrets

For scientific or large‑scale work, NumPy offers vectorized random generation that is much faster and provides many distributions. Example:

import numpy as np
arr = np.random.rand(1000)  # 1000 floats in [0,1)
int_arr = np.random.randint(1, 100, size=50)  # 50 integers

For security‑sensitive randomness (e.g., generating a password reset token), use the secrets module:

import secrets
secure_ints = [secrets.randbelow(100) for _ in range(5)]  # 0-99

The secrets module uses the operating system's cryptographic random source, which is not predictable.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.