Beginning The Nature Of Code

To carry on from the Processing work we did in class, I began to read the book The Nature of Code and start some of the exercises within it. The aim of the book is to teach the reader how to program how to model a digital “ecosystem” using concepts from biology and physics. I chose to look into this book to learn some more advanced programming skills and hopefully I can complete most of the assignments.

To start off the book talks about normal distribution and how in nature variable information usually lies along a bell curve. For example take people’s heights. The height of people can vary greatly, but if you looked at everyone’s heights as a data set, more people would be near the average height than at the two extremes. The mathematics behind this is called Gaussian distribution.

Gaussian distribution needs mean and standard deviation to calculate the values. In Processing this is pretty straightforward as there is a library to calculate a random number based on Gaussian distribution and we can set the mean and standard deviation for this ourselves.

Mean is the sum of all the data divided by the number of items. Standard deviation is the average of the squares of each values deviance from the mean.

The equations for this are included in the Java library and a version of this is included in Processing. Take for example this code which uses Gaussian distribution to place white dots around a particular point.

import java.util.*;
Random generator;

void setup() {
  generator = new Random();
  background (12, 13, 14);

void draw(){
  float xnum = (float) generator.nextGaussian();
  float xsd = 30;
  float xmean = 320;
  float ynum = (float) generator.nextGaussian();
  float ysd = 42;
  float ymean = 150;
  float x = xsd * xnum + xmean;
  float y = ysd * ynum + ymean;
  fill(255, 10);

The lines generator.nextGaussian() creates a Gaussian distribution with ysd and xsd being the standard deviation and xmean and ymean being the mean. The circles are semi transparent so one can see the brighter areas near the middle where more of the dots are clustered, whereas on the outside the dots are more faint as they are less likely to be placed there. The overall pattern is longer than it is wide as the standard deviation for the y axis is bigger than for the x axis.

Shiffman, D., 2012. The Nature of Code [online] Mountain View: Creative Commons.

Leave a Comment