Random Walk
The random walk is a mathematical model of a path of random steps. It is used to describe many processes in nature, like the path of a foraging animal or the motion of a gas particle. Our first assignment for Nature of Code was to construct a program in Processing that demonstrates this behavior.
For my program, I altered Dan Shiffman’s random walk example to have multiple walker objects (i.e. many random walk paths are generated simultaneously from different origins). At each point along the path, Processing draws a new arc. There are exactly 51 walker objects, since the red value of the arc color increments by 5 with each iteration and the maximum red value is 255.
Here’s the code for main program:
[code lang="java"]Walker[] w; void setup() { size(600,600); background(3, 57, 90); colorMode(RGB,255,255,255,255); //51 walker objects - red value increases by 5 until it hits 255 w = new Walker[51]; for(int i=0; i < w.length; i++) { w[i] = new Walker(i*5, i*10, i+height/2); } frameRate(60); //background(0); } void draw() { noStroke(); for(int i=0; i < w.length; i++) { w[i].walk(); w[i].render(); } }[/code]
This is the code for the walker object:
[code lang="java"] class Walker { int x,y; int r; Walker(int r_, int x_, int y_) { x = x_; y = y_; r = r_; } void render() { fill(r, 120, 0); arc(x, y, 10, 10, PI, 2*PI); } // Randomly move up, down, left, right, or stay in one place void walk() { int vx = int(random(3))-1; int vy = int(random(3))-1; x += vx*5; y += vy*8; x = constrain(x,0,width-1); y = constrain(y,0,height-1); } } [/code]