liveBook · Manning

Random seed

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A random seed is an initial value that determines the starting state of a pseudorandom-number generator.

A random seed is an initial value that starts a pseudorandom-number generator. Reusing the same seed produces the same sequence, making generated data reproducible. section 1.3

Recreate the same random data

Create two NumPy generators with seed 0 and draw the same ten integers. The resulting arrays match, so both exercises can be compared reliably. Use the generated values to construct a pandas Series. section 1.3

g = np.random.default_rng(0)    #1
a = g.integers(0, 100, 10)      #2
 
g = np.random.default_rng(0)    #3
b = g.integers(0, 100, 10)      #4
 
a == b                          #5

This listing demonstrates that resetting np.random.default_rng(0) reproduces the same sequence of integers. section 1.3

Prefer passing the seed to np.random.default_rng and working with its generator object. The older np.random.seed function still exists, but NumPy developers discourage using it. section 1.3

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Defines reproducibility and demonstrates it with repeated integer draws, without explaining pseudorandom-generation internals.
Examples
Uses np.random.default_rng(0) to generate identical arrays and seeded integers for a monthly Series.
What it assumes
Assumes familiarity with NumPy random generation and pandas Series rather than teaching either from zero.
What you can do after
Apply it via np.random.default_rng to reproduce generated NumPy arrays and pandas test data.
How it connects
Connects random seeds with NumPy generators, random integers, and pandas Series.

Also explained in

Compare how 2 books explain random seed →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.