Random Walkers

Random Walkers

September 30, 2016

example processing animation arraylist random-walker generative-art emergence

This example takes the previous Random Walker example and uses an ArrayList instead of an array.

ArrayList<RandomWalker> randomWalkers = new ArrayList<RandomWalker>();

void setup() {
  size(200, 200);



void draw() {
  for (RandomWalker rw : randomWalkers) {

void mouseDragged() {
  randomWalkers.add(new RandomWalker());

class RandomWalker {
  float x = mouseX;
  float y = mouseY;
  float r = random(256);
  float g = random(256);
  float b = random(256);

  void step() {

    x += random(-1, 1);
    y += random(-1, 1);

    if (x < 0) {
      x = width;
    if (x > width) {
      x = 0;

    if (y < 0) {
      y = height;
    if (y > height) {
      y = 0;

  void draw() {
    stroke(r, g, b);
    point(x, y);

random walkers

Code Editor

See the Pen by Happy Coding (@KevinWorkman) on CodePen.

Tweak Ideas

  • Make it so the random walkers die when they exit the screen by removing them from the ArrayList.
  • Make it so the random walkers die after a certain number of steps.
  • Base the movement direction of each random walker off the direction the mouse is moving when the user clicks.

ArrayLists Examples


Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!