← Games

Game Hub Articles

All articles
Home › Articles
🎲

How games use random numbers (and why they're not really random)

By Noam · 26 November 2026 · 7 min read

Almost every game uses randomness: shuffling cards, placing mines, choosing the next falling block, deciding where food appears in Snake. But computers follow exact instructions, so how can they do anything random? The answer is that most of the time, they don't, and they don't need to.

Pseudo-random numbers

Most games use a pseudo-random number generator (PRNG). It's a formula that takes a number, scrambles it with some maths, and produces a new number that looks random. Then it uses that number to make the next one, and so on. The results pass lots of tests for randomness, but they're completely predictable if you know the formula and the starting number.

In JavaScript, Math.random() gives a number between 0 and 1. To roll a dice:

const roll = Math.floor(Math.random() * 6) + 1;   // 1 to 6

Seeds

The starting number of a PRNG is called the seed. The same seed always produces the same sequence. That sounds like a problem, but games use it on purpose:

Shuffling fairly

Shuffling looks easy, but many simple methods are secretly unfair, making some orders more likely than others. The standard fair method is the Fisher–Yates shuffle: go through the deck from the end, and swap each card with a random card at or before it.

function shuffle(deck) {
  for (let i = deck.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [deck[i], deck[j]] = [deck[j], deck[i]];
  }
  return deck;
}

Every possible order comes out equally likely. A deck of 52 cards has so many possible orders (a number with 68 digits) that a well-shuffled deck is almost certainly in an order no deck has ever been in before.

Random isn't always fun

Pure randomness can feel unfair. Ten bad results in a row can happen by chance, and players hate it. So many games bend the rules a little:

Truly random numbers

When randomness really matters, like for passwords and security, computers collect unpredictability from the real world: tiny timing differences, electrical noise and other physical sources. Browsers provide this through crypto.getRandomValues(). For games, Math.random() is perfectly fine.

Try it

Every new game of Solitaire starts with a shuffle like the one above. If you're learning to code, try writing a dice-rolling program that rolls 6,000 times and counts each number. Each should come up close to 1,000 times.

▶ Play SolitaireAll games

More articles

🪩 Pinball tips: how to trap the ball, aim your shots and score moreStop flailing at the flippers. Learn to catch the ball, aim with the flipper, pass it between flippers and use nudges, plus a bit of pinball history.🐍 Snake strategy: how to get a really long snakeHug the walls, sweep in zig-zags and never trap yourself. Simple strategies for the classic Snake game, plus where Snake came from.🎳 Bowling tips and how bowling scoring really worksWhy strikes and spares are worth extra, how a 300 game works, and where to aim to knock down all ten pins.🧱 Brick-breaking games: the story of Breakout and how to clear every levelHow a 1976 arcade game built by two future Apple founders created a whole genre, and how to control the ball's angle to clear bricks faster.🟦 Falling-block puzzle tips: keep your stack flat and clear more linesFlat stacks, a well for the long piece, using Hold and Next, and staying calm at high speed. How to get better at falling-block puzzles like Block Drop.