Portfolio Header
Priyanshu Samal
Profile Picture
Back
336

Visualizing Probability: Random Walk in C

I’ve been building a random-walk simulation in C using SDL, mostly to understand what actually happens when you let simple rules run over time.

2026-01-08·8 min read
Visualizing Probability: Random Walk in C

I’ve been building a random-walk simulation in C using SDL recently, inspired by Daniel Hirsch's YouTube channel, mostly to understand what actually happens when you let simple rules run over time.

Each “agent” starts at the same point and moves randomly, one step at a time. At first it looks like chaos, but pretty quickly a pattern shows up: everything keeps piling up near the center, while only a few paths manage to drift outward. That concentration isn’t a rendering issue or a logic bug it’s the math. In 2D, random walks are recurrent, meaning that walkers continually revisit their starting point. Most steps cancel each other out, so distance grows slowly, not explosively.

The Power of Trails

What made this click for me was adding trails. Once you stop clearing the screen and let history accumulate, the simulation stops being about motion and starts becoming a visualization of probability density. Areas that get visited often stay bright; areas that don’t just fade away. Even though each agent has its own color, overlapping paths naturally mash them together at the center because that’s where the system statistically spends most of its time. The result feels less like drawing dots and more like watching the math expose itself frame by frame.

It also puts things into perspective. This isn’t AI, noise functions, or fancy shaders, just memory, randomness, and time. And yet you get structure, bias, and emergent behavior for free. The more I work on these low-level experiments, the more I realize how much insight you get just by letting simple systems run long enough and actually looking at what they’re doing.

Code Walkthrough

#### 1. The Agent Struct

typedef struct {
    int x;
    int y;
    Uint8 r, g, b;
} Agent;

Each "walker" has a position (x, y) and a unique color (r, g, b). We don't need a velocity vector because they move instantly in cardinal directions.

#### 2. Initialization (The Color Gradient)

for (int i = 0; i < agent_count; i++) {
    agents[i].x = WIDTH / 2;
    agents[i].y = HEIGHT / 2;

    float t = (float)i / agent_count;
    agents[i].r = (Uint8)(255 * t);
    agents[i].g = (Uint8)(255 * (1.0f - t));
    agents[i].b = (Uint8)(128 + 127 * sin(t * 6.28f));
}

* Position: All agents start at the center.

* Color: We use a parametric equation to assign a gradient. t goes from 0.0 to 1.0. Red increases, Green decreases, Blue oscillates (sine wave). This ensures every agent looks distinct.

#### 3. The Fade Effect (Visual Trick)

Instead of clearing the screen with SDL_RenderClear (which wipes everything to black), we do this:

SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 25);
SDL_Rect fade = { 0, 0, WIDTH, HEIGHT };
SDL_RenderFillRect(renderer, &fade);

* Blend Mode: Enables transparency.

* Alpha 25: We draw a black rectangle that is only ~10% opaque.

* Result: New pixels are bright. Old pixels get slightly darker every frame until they vanish. This creates the "tail" effect without storing history.

#### 4. The Movement Logic

int dir = rand() % 4;

if (dir == 0) agents[i].x += 2;
if (dir == 1) agents[i].x -= 2;
if (dir == 2) agents[i].y += 2;
if (dir == 3) agents[i].y -= 2;

We toss a 4-sided die (rand() % 4). The agent moves 2 pixels in that direction. We then clamp the coordinates to the window bounds to keep them on screen.

View Demo on X (Twitter)View Source Code on GitHub

TechCSDL2SimulationMath
      |\__/,|   (`\
    _.|o o  |_   ) )
-(((---(((--------
~ git commit -m "bye"_