Kamel Ghabte · Course · Free

Random Walk

Explore random walk and its visual traces in p5.js.

The concept

A random walk is a process where an agent moves in successive random directions. It's fundamental in physics (Brownian motion), finance, and generative art. By accumulating traces, organic structures emerge.

The code

// Random walk with colored trace
let x, y;
function setup() { createCanvas(400, 280); background(11,15,20); x=width/2; y=height/2; }
function draw() {
  for (let i=0; i<10; i++) {
    let px=x, py=y;
    x += random(-3, 3); y += random(-3, 3);
    x = constrain(x, 0, width); y = constrain(y, 0, height);
    stroke(55, 227, 195, 80); strokeWeight(1.5);
    line(px, py, x, y);
  }
}

Going further

Explore other free courses on the Learn page, or book a call to discuss your project.

Want to go further?

Book a discovery call →