Computers don’t actually get lucky. They are deterministic machines. They follow instructions. To simulate luck, they use two distinct methods to generate numbers. One relies on the chaos of the physical world. The other relies on math.
The first method is True Random Number Generation (TRNG). This involves hardware that monitors a natural, unpredictable event. You might place radioactive material in front of a Geiger counter connected to a PC. Radioactive decay is chaotic. It’s truly random. The computer reads these events. It turns them into numbers. This approach works. It’s just rare. Most people don’t have Geiger counters plugged into their laptops.
The Math Behind Pseudo-Randomness
Most of the time, you are dealing with pseudo-random numbers. This is what software generates. It uses a formula. The goal? To create a sequence that looks random to someone who doesn’t know the trick.
A good formula needs three things:
- No repetition. It shouldn’t cycle back on itself quickly.
- Good distribution. If you ask for numbers between 0 and 9, you should see roughly equal amounts of each digit over time.
- Lack of predictability. You can’t guess the next number unless you know the exact formula and the starting point.
The Classic C Example
Brian Kernighan and Dennis Ritchie gave us a famous example in The C Programming Language. It’s simple. It’s effective.
This code uses a variable called random_seed. You start with a number. The code multiplies it by 1,103,515,245. Adds 12,345. Replaces the old seed with this new value. Then it spits out a number.
The distribution is solid. It doesn’t repeat often. If you seed it with 10, the first 20 outputs might look like this:
4 4 6 0 7 4 2 3 5 0 5 6 6 4 5 6 7 6 7 4
Not very random-looking. But look at the big picture. If you run this 10,000 times for digits 0-9, you get:
- 0: 1,015
- 1: 1,024
- 2: 1,048
- 3: 996
- 4: 988
- 5: 1,001
- 6: 996
- 7: 1,006
- 8: 965
- 9: 961
It’s close to equal. That’s the point.
The Seed Matters
Here’s the catch. The whole system depends on the seed. This is the initial value. If you start with the same seed, you get the exact same sequence. Every time. On any computer.
This isn’t a bug. It’s a feature for some systems. The Global Positioning System (GPS) uses this reproducibility. Satellites need predictable but distinct patterns. The receiver tracks them using this known behavior.
Making It Unpredictable
For most apps, you want unpredictability. You don’t want the same sequence every time you launch an app. So how do you get a truly random seed?
Programs usually grab the current date and time. Often converted to an integer. Like the number of seconds since January 1, 1970. This number changes constantly. It’s different every time you start the program. It makes a decent seed.
It’s not perfect. True randomness is hard to fake. But for games, simulations, and general software, pseudo-random number generators (PRNGs) do the job.
Does Google have a random number generator? Yes. They do. But the underlying principle remains the same. Math stands in for chaos. Until the next breakthrough in hardware entropy.
