Random Module in NumPy

Python · NumPy Library

Theory

NumPy generates random numbers through a Generator: rng = np.random.default_rng(42). The number is a seed; the same seed always produces the same sequence, which makes experiments and examples reproducible. rng.integers(1, 7, size=5) rolls five dice (the high value is excluded), rng.random(3) gives floats from 0 to 1, rng.normal(loc=50, scale=10, size=1000) samples a bell curve with mean 50 and standard deviation 10, and rng.uniform(a, b, size) spreads values evenly.

rng.choice(arr, size=3, replace=False) picks items without repeats, and p=[...] sets the probability of each item. rng.shuffle(arr) reorders an array in place, while rng.permutation(arr) returns a shuffled copy. Older code uses np.random.seed(42) and np.random.randint; it still works, but the Generator API is the recommended style.

Why we use it

Data work needs randomness for sampling, shuffling, simulations and creating test data. A seeded generator makes those results repeatable, so you and your readers get the same numbers every run.

When to use it

Use default_rng with a fixed seed in tutorials, tests and experiments; leave the seed out when you want different results each run. Use choice for sampling, normal for simulated measurements and shuffle before splitting data by hand.

How it works

  1. rng = np.random.default_rng(42)
  2. Need whole numbers? Yes: rng.integers(low, high, size). No: Choose a float distribution.
  3. Bell-shaped data? Yes: rng.normal(loc, scale, size). No: rng.random or rng.uniform.
  4. Sample or reorder with rng.choice / rng.shuffle
  5. Same seed gives the same results every run

Syntax

rng = np.random.default_rng(seed)
rng.integers(low, high, size)
rng.random(size)  rng.normal(loc, scale, size)
rng.choice(a, size, replace=False, p=None)
rng.shuffle(a)  rng.permutation(a)

Example

rng = np.random.default_rng(42)
print(rng.integers(1, 7, size=5))   # five dice rolls

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic