NumPy Random Generator — ELI5

Imagine you have a magic dice-rolling machine. You press a button and it spits out a number. It looks random — you cannot predict the next number by watching the previous ones. But here is the secret: the machine follows a hidden recipe. If you start the machine the same way twice, it rolls the exact same sequence.

That is how NumPy generates random numbers. It uses a recipe called an algorithm that produces numbers that look random but are completely predictable if you know the starting point. The starting point is called a seed.

Why is fake randomness useful? Because you can reproduce results. If a scientist runs an experiment using random data and gets an interesting result, they can share the seed and anyone can rerun the experiment and get the same numbers. Real randomness would make that impossible.

NumPy’s new way of doing this is np.random.default_rng(). You give it a seed number, and it hands back a “generator” — your personal dice-rolling machine. Each generator is independent, so you can have several running at the same time without them interfering.

The old way (np.random.seed() and np.random.rand()) still works but is like having one shared machine for everyone in the office. The new way gives each person their own.

The one thing to remember: NumPy’s random numbers follow a hidden recipe — set the seed and you can replay the exact same “random” sequence anytime.

pythonnumpydata-science

See Also