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.
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:
Now the real thing. Run the cells.
- 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 > 60is a mask ofTrueandFalse. Its mean is the share that areTrue, becauseTruecounts as 1 andFalseas 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.histogramcounts 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.
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
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.
- NumPy: Random sampling (numpy.random): default_rng, and why it replaced the old np.random functions
- NumPy: Random Generator: integers, uniform, normal, choice and shuffle
- NumPy: numpy.histogram: counting values into bins
- NumPy: Permuted Congruential Generator (64-bit, PCG64): the method default_rng uses to make its numbers