NumPy · lesson 10 of 13
Parts in this lesson
Arng = np.random.default_rng(7)a generator, with a seed
Brng.normal(48, 6, size=n)numbers around 48, spread by 6
C(days > 60).mean()the share of days where it’s true

Random numbers and simulation

Last updated

Sometimes the data you need doesn't exist yet, or you want to know what chance alone could do. Then you make data up, carefully, with random numbers. In this lesson you'll draw random numbers of different kinds, simulate 10,000 days of sales, and make every run give the same answer with a seed.

By the end of this lesson you can

  • Make a random number generator with np.random.default_rng
  • Draw whole numbers, uniform and normal numbers, and random choices
  • Simulate many days of sales and answer a question from the simulation
  • Make a simulation repeatable with a seed

A generator

NumPy's random numbers come from a generator. You make one with np.random.default_rng, and ask it for numbers. Run the cells.

random.ipynb
  • rng.integers(1, 7, size=10): ten whole numbers from 1 up to but not including 7. Ten dice.
  • rng.uniform(0, 1, size=3): decimals between 0 and 1, every value equally likely.
  • rng.normal(48, 6, size=5): numbers from a normal distribution with mean 48 and standard deviation 6 (lesson 2.8). Most land near 48; about two-thirds land within 6 of it.
  • rng.choice(list, size=5): picks from a list, here five store names.
  • rng.shuffle(order) shuffles an array in place: a random order to visit six stores.

You'll find older code that writes np.random.rand() or np.random.seed(...). Those still work, but NumPy's documentation recommends default_rng for new code.

10,000 days of sales

A store sells about 48 kg of rice a day, give or take 6. How often does it sell more than 60 kg? You could wait years to find out, or simulate. First, what 10,000 simulated days look like:

Random days dropping into bins. 10 days of simulated sales, each a random number around 48 kg. With so few, you can’t tell what shape they come from.10 days30343842465054586266
1 of 4 10 days of simulated sales, each a random number around 48 kg. With so few, you can’t tell what shape they come from.

Now the real thing. Run the cells.

simulate.ipynb
  • The mean and standard deviation of the 10,000 days come out at 47.93 and 5.97, close to the 48 and 6 asked for, but not exactly. Random samples never match exactly; the bigger the sample, the closer they get.
  • days > 60 is a mask of True and False. Its mean is the share that are True, because True counts as 1 and False as 0. So about 2.2% of days, around 8 days a year, sell more than 60 kg. That's an answer for how much spare stock to keep.
  • np.histogram counts how many days fall in each 4 kg bin. Those are the counts in the last frame of the figure. Stage 6 draws them as a chart.

Wrong way: no seed

Run the first two cells, and compare.

seed.ipynb

Two different answers from the same line. Run them again: two more. A generator with no seed starts somewhere new each time. That's fine for a game, but not for analysis: if your notebook says "2.2% of days" and a colleague reruns it and gets 2.4%, which is right? Neither of you can check the other.

The last two cells pass a seed, 7, and give the same three numbers every time, on every computer. A seed is a starting point: the same seed gives the same sequence. Set one, once, at the top of any notebook that uses random numbers, and every run will give the same results. Which number you pick doesn't matter; that you write it down does.

Check what you learned

rng.integers(0, 2, size=100) gives…
wet = rng.uniform(0, 1, size=365) < 0.3. What does wet.mean() tell you?
Your notebook simulates sales. Why set a seed?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis