Sunday, September 25, 2016

Random Walks - Creative Coding

Random walks are one of the first graphically interesting programs that many people will make, having rules that are simple and immediate results makes it a great piece of low hanging fruit. I have written up a random walk every time I learn a new language and always felt a little underwhelmed at the results, usually an amorphous black blob with jagged edges, there has to be a way to make a random walk look a little nicer.

I started my random walk with the standard set of rules:

10:Roll a random number from 0 to 3
20:move up, down, left or right depending on the number
30:draw a pixel
40:goto 10

I quickly had to add some limits to stop the program from writing off the edge of the screen and to stop the walk if it goes past a set number of steps, usually a few tens of thousands. Then I made a 2D array the same size as my output screen, I used this to keep track of how many times the random walk visited each cell, and another array was used to keep track of the order in which the steps were taken. Now that I had some information about each step on the random walk I could use it to generate some more interesting output.

As each cell is drawn two numbers, each being between 0 and 1 inclusive, are generated representing the value, relative to all the other cells, for each cell's number of visits and position from beginning to end of the walk. To make the internal details of the walk clearer I also increased the space in between each cell, this also allowed me to use the size of each point as another output variable. By varying the size, colour, saturation and brightness of each step, based on the cell's position in "time" and how many times it was visited I was finally able to get the interesting random walk I have been waiting over a decade to see. (Check out the full sized version on in my Flickr gallery)

No comments:

Post a Comment