How Does Pseudorandom number generators | Computer Science | Khan Academy Work?

A pseudorandom number generator is a computer’s way of picking numbers that feel random by following a secret, repeating pattern you can’t see.

Imagine you have a giant spinning wheel with numbers on it. When you spin it, the ball lands in one spot. It looks like chance, but if you knew exactly how fast the wheel was turning and where the ball started, you could predict exactly where it would land. That is what a PRNG does inside your computer.

The Secret Recipe

Computers are very literal. They don’t have true luck; they have recipes called algorithms. A PRNG takes a starting number (called a seed) and mixes it using a simple math recipe over and over to create a new list of numbers.

Think of making chocolate chip cookies. You start with dough, add chips, bake them, and get a batch. If you use the exact same bowl size, oven temperature, and amount of flour every time, your next batch will be identical. The computer does this with math. It starts with one seed number, runs it through a formula like multiply by 110352212, and uses the result as both an output number and the new starting point for the next turn. This creates a long string of numbers that look scattered but are actually following a strict rule.

Why It Matters

This trick is perfect for video games or card shuffling. If you play a game on level one, and then restart it, you want to be able to start from the same place so you can win again exactly as before. The PRNG remembers its pattern perfectly. It isn’t truly random like dice rolling in the wind, but for our screens, that is close enough! It gives us variety without losing control.

Take the quiz →

Examples

  1. picking names from a hat with patterns
  2. shuffling a deck of cards
  3. game dice rolls that look random

Ask a question

See also

Loading…

Discussion

Recent activity