Random numbers with NumPy · Python Programming Class 11–12 | StemPy Lab
Course 3.1·Lesson 1 of 5
Random numbers with NumPy
A computer makes random numbers with a formula, so they are pseudo-random, and a generator made with np.random.default_rng and a seed gives the same numbers every time, which makes an experiment repeatable. rng.random gives decimals from 0 up to 1, rng.integers gives whole numbers such as the faces of a die, rng.uniform a range, rng.normal numbers from a bell curve with a given centre and spread, rng.choice a pick with optional weights and without replacement, and rng.shuffle changes an order. np.bincount counts the faces. The share of an outcome comes closer to its probability as the number of trials grows, which is the law of large numbers: 600 rolls give shares within about 0.04 of one sixth, and a million rolls within 0.0002.
about 40 min1 simulation
Lesson 1 of 5Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
After this lesson you can
make random numbers of several kinds with a NumPy generator,
repeat a random experiment exactly by giving a seed, and
see the law of large numbers in a simulation.
Why simulate?
Chance is part of science. A radioactive atom decays at a random moment. A measurement has a random error. The molecules of a gas move at random. A computer can imitate chance: you let it roll the dice thousands of times in a second and count what happens. This is called a simulation. It answers questions that are hard or impossible to answer by calculation, and it lets you see how the randomness behaves.
A random number generator
A computer is not random: it follows a program. To make numbers that look random, it uses a formula that produces a long sequence that has no pattern that you can see. Such numbers are called pseudo-random. In NumPy, you make a generator and ask it for numbers.
Predict first. The program makes two generators with the seed 42. Will they give the same numbers? Then run it.
A die rolled many times
A bar chart titled 600 rolls of a die. Six blue bars for the faces 1 to 6 have the heights 109, 85, 103, 93, 107 and 103, each with its number written on top. A dashed black line at 100 marks the expected number for a fair die. The bars are close to the line but none is exactly on it.
Predict first. For a fair die, how many times will each face come up in 600 rolls? Will it be exactly 100 each time?
The usual scatter of a count is about 600×61×65=9.1. So a count between about 91 and 109 is normal. Our counts, 85 to 109, are a little wide, but still in the normal range (about two spreads).
The law of large numbers
Predict first. The chance of a six is 1/6=0.1667. How close will the share of sixes be after 10 rolls? After a million?
Notice that the improvement is slow. A hundred times more rolls bring the distance down by only about ten times. We return to this in the next lesson.
The code's count of sixes wobbles from seed to seed, but almost always inside the red bar.
Other kinds of random numbers
Common mistakes
Using the old np.random.seed. The new way is rng = np.random.default_rng(seed).
Making a new generator in every loop with the same seed. You get the same numbers each time. Make one generator and use it again.
Forgetting that the upper limit of integers is not included. For a die, write integers(1, 7).
Expecting a random result to be exactly the average. Random counts scatter.
Leaving out the seed and then being unable to repeat a result.
Practice
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Warm-up. Open the lab Roll the dice. You write functions for the rolls of a die and the share of a face.
Build. Open the problem Reproducible and the problem Fair or not?
Remember these
rng = np.random.default_rng(seed) makes a generator. The same seed gives the same numbers.
rng.random, rng.integers, rng.uniform, rng.normal, rng.choice and rng.shuffle make the common kinds of random numbers.
Counts of random events scatter around their expected values.
Law of large numbers: with more trials, the share of an outcome comes closer to its probability.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Why give a seed in a simulation?
What does rng.integers(1, 7, size=5) give?
In 600 rolls of a fair die, about how many sixes do you expect?
What is the difference between replace=True and replace=False in choice?
Enroll free in Simulation and Randomness
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.