Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Module

np.random

numpy.random provides pseudo-random number generators and distribution sampling for NumPy. The modern workflow is to create a Generator with default_rng, optionally seed it for reproducible results, and call its sampling methods.

How it works

The module provides pseudo-random number generators and distribution-sampling utilities. Create a Generator with default_rng, optionally provide a seed for reproducibility, and use its methods to produce scalar or array samples.

Syntax

numpy.random

Key points

  • numpy.random implements pseudo-random generators rather than true hardware randomness.
  • Its API can sample from many probability distributions.
  • The recommended entry point is creating a Generator through default_rng.
  • An integer seed makes the generated sequence reproducible.
  • Generator methods can produce uniform floating-point values in the half-open unit interval.
  • The module targets simulation and statistical modeling workloads.

Common mistakes

  • Do not use numpy.random for security-sensitive or cryptographic randomness.
  • Calling default_rng without a seed normally produces a different sequence between runs.

Example: Reproducible integer sampling

import numpy as np

rng = np.random.default_rng(42)
values = rng.integers(0, 10, size=5)
print(values)

Create a reproducible generator with a seed, then draw five uniformly distributed integers from 0 through 9.

When to use it

Reproducible experiments

Generate repeatable test fixtures, randomized examples, or simulations.

Statistical simulation

Draw values from uniform and other probability distributions for numerical models.

Array sampling

Create random arrays for algorithms, benchmarks, and exploratory numerical work.

Interview questions

QWhat is numpy.random used for?

numpy.random supplies pseudo-random number generators and sampling methods for many probability distributions. The recommended pattern is to create a Generator with default_rng and call methods on that object.

Follow-up: Why use a Generator instead of relying on module-level functions? A Generator gives explicit state ownership and supports the modern random-number API.

QHow do you make generated samples reproducible?

Pass an integer seed to default_rng, such as default_rng(42), to initialize a reproducible deterministic sequence. Reusing the same seed and generation sequence reproduces the results.

Follow-up: What happens without a seed? The generator is initialized from operating-system data, so repeated runs generally produce different sequences.

QCan numpy.random generate secure tokens?

The module's generators are intended for statistical modeling and simulation, not cryptographic use. Security-sensitive applications need a security-oriented random source instead.

Follow-up: Would a larger seed make it cryptographically secure? No; changing the seed does not change the generator's suitability for security or cryptography.

  • numpy.random.default_rng
  • numpy.random.Generator
  • numpy.random.integers
  • numpy.random.random

Source: Random sampling (numpy.random). Checked 2026-10-15.