Computer Science • Algorithms

Procedural Generation Basics: Beyond Randomness

Written by Sudhishkumar K • Jan 2026 • 25 Min Read

How did a tiny team at Hello Games build the 18 quintillion planets of No Man's Sky? How does Minecraft generate an infinite world that always feels playable? How does Spelunky ensure that every single random level is actually beatable?

The answer is Procedural Content Generation (PCG). It is the holy grail for indie developers: using code to create content, allowing small teams to build massive, infinitely replayable games without hiring 500 artists.

However, many beginners misunderstand PCG. They think it means "making things random." This is a recipe for disaster. True procedural generation isn't about chaos; it's about controlled randomness structured by strict rules.

In this deep dive, we will move from basic randomness to structured grid systems, Perlin Noise, Cellular Automata, and the advanced Wave Function Collapse algorithm.

Level 1: The Trap of Pure Randomness (White Noise)

When a beginner tries to generate a level, their first instinct is to use a basic random number generator to scatter objects.


// The Naive Approach
for (int i = 0; i < 100; i++) {
    Vector3 randomPos = new Vector3(
        Random.Range(0f, 50f), 0, Random.Range(0f, 50f)
    );
    Instantiate(treePrefab, randomPos, Quaternion.identity);
}
            

Why this fails:
In math, this is called "Uniform Distribution" or "White Noise." It creates:
1. Clumping: Trees will spawn inside other trees.
2. Gaps: Massive empty fields will appear randomly.
3. No Context: A tree might spawn underwater or inside a rock.

This isn't design; it's static. To fix this, we need structure.

Level 2: Structured Grid Systems

The easiest way to add structure is to use a grid. Think of a chessboard. Instead of placing an object at float coordinate `(12.43, 5.21)`, you place objects in integer slots like `[2, 5]`.

This guarantees that objects never overlap (if you check the slot first).

Cellular Automata: Making Caves

The most famous grid-based algorithm is Cellular Automata (derived from Conway's Game of Life). It is perfect for generating organic-looking caves.

The Cave Algorithm

  1. Fill: Fill the grid randomly. 45% Wall, 55% Empty.
  2. Smooth: For each cell, count its neighbors.
  3. Rule: If a cell has more than 4 wall neighbors, it becomes a wall. Else, it becomes empty.
  4. Repeat: Run step 2 and 3 for 5 iterations.

This simple rule causes the random noise to "clump" together into smooth, large caverns, removing the jagged single blocks.

Level 3: The Industry Standard - Perlin Noise

To generate terrain (heightmaps), we need "Coherent Noise." This means that if point A is high, point B (next to it) should also be relatively high.

Perlin Noise generates smooth gradients. However, raw Perlin noise looks like smooth rolling hills. Real mountains have jagged rocks and large slopes. To achieve this, we layer noise using Octaves.


float GetLayeredNoise(float x, float y) {
    float amplitude = 1;
    float frequency = 1;
    float noiseHeight = 0;

    // We layer 3 different noise maps on top of each other
    for (int i = 0; i < 3; i++) {
        float sampleX = x * frequency;
        float sampleY = y * frequency;
        
        // Mathf.PerlinNoise returns 0.0 to 1.0
        float perlinValue = Mathf.PerlinNoise(sampleX, sampleY);
        
        noiseHeight += perlinValue * amplitude;

        amplitude *= 0.5f; // Persistence: Each layer has half the impact
        frequency *= 2.0f; // Lacunarity: Each layer is twice as detailed
    }
    return noiseHeight;
}
            

The Terminology:
Frequency: How "zoomed in" the noise is. High freq = many small bumps.
Amplitude: How "tall" the bumps are.
Lacunarity: How much frequency increases per layer (usually 2.0).
Persistence: How much amplitude decreases per layer (usually 0.5).

Level 4: Voronoi Diagrams (Biomes)

How do you decide where the "Desert" ends and the "Forest" begins? You can't just draw a straight line.

Voronoi Diagrams are used to partition space into regions.

  1. Pick 10 random points on the map. These are "Biome Centers."
  2. For every pixel on the map, calculate which Biome Center is closest.
  3. Assign the pixel that Biome's color.

This creates cellular, organic-looking regions that fit together perfectly like a puzzle. It is used for political maps in strategy games (like Civilization) and biome generation in Minecraft.

Level 5: Wave Function Collapse (WFC)

This is the cutting edge of PCG, popularized by the indie hit Townscaper. WFC doesn't deal with heights; it deals with Constraints.

Imagine a Sudoku puzzle. You don't guess numbers; you look at the rules. "This cell cannot be a 5 because there is a 5 in this row."

WFC works similarly for map tiles:

1. Define Rules "Water tile cannot touch Grass tile."
"Road tile must connect to another Road tile."
2. Entropy The algorithm calculates "Entropy" for every slot. Entropy = How many options are still possible here?
3. Collapse Pick the slot with the lowest Entropy (fewest options) and force it to pick a state.
4. Propagate Because that slot is now decided, update the possibilities of its neighbors. Repeat until map is full.

The Secret Ingredient: Seeding & Determinism

The most important technical concept in PCG is the Seed.

Computers cannot generate true random numbers. They use "Pseudo-Random Number Generators" (PRNG). If you give the generator a starting number (the Seed), it will produce the exact same sequence of random numbers every time.

In Unity:
Random.InitState(12345);

If you run your level generation script after setting this state, you will get the exact same forest, same caves, and same loot drops.

Why is this vital?
1. Sharing: Players can share "Cool seeds" with friends (like in Minecraft).
2. Debugging: If a player finds a bug in a generated level, they can send you the seed, and you can recreate that exact level on your machine to fix it.
3. Save Files: You don't need to save the position of every tree (which would be a massive file). You just save the Seed (an integer). When the player loads the game, you regenerate the world using the seed.

Conclusion: The Hybrid Approach

Procedural generation has a massive downside: Fatigue. Humans are excellent pattern recognition machines. After seeing 100 generated planets, players see the math. "Oh, it's just this biome mixed with that color palette."

The best approach in modern development (used by Spelunky and Hades) is Procedurally Placed Hand-Crafted Content.

Don't generate every single room atom by atom. Instead, hand-craft 50 beautiful rooms. Then, use an algorithm to decide how those rooms connect and map together. This gives you the infinite scale of algorithms with the intentional design of a human artist.


Frequently Asked Questions (FAQ)

Q: Is PCG harder than manual design?
A: Yes. Writing a generator takes 10x longer than building one level. It only pays off if your game needs 100+ levels. For a short story game, do not use PCG.

Q: Can I use Perlin Noise for textures?
A: Yes! You can generate wood grain, marble, or cloud textures procedurally at runtime to save texture memory.

Q: What is "Blue Noise"?
A: Blue Noise is a type of random distribution where points are evenly spaced (no clumping), but still random. It is used for placing trees or vegetation to look natural but uniform. It is computationally expensive to generate (Poisson Disk Sampling).