HOME
Preface A Model A Medium A History Variables Functions Control Loops Classes
Source Overview

Control

“Randomness is often used to “humanize” or introduce variation and imperfections to an underlying rigid, deterministic process…”

Ten Questions Concerning Generative Computer Art. McCormack, Jon et al.


The Structured Programming Paradigm

Most modern day computer languages share a limited number of common structures which can be broken down into three main common parts : sequencing, selection and iteration. These three parts make up what is called the Structured Programming Theorem, stating that the combination of these three structures is sufficient for expressing any computable process within a program. Historically, this theorem was developed in the 1960s with languages such as Algol and Pascal, replacing the need for the famous GOTO statement which, for some computer scientists, was a cumbersome manner for controlling the flow of a program.

Learning the relationship between these structures and the procedures involved to write them is vital if you want to fully understand the behaviour of the whole process you are attempting to control in a programming environment. Over time, it also becomes increasingly important to master the various idioms of these structures. They are the building blocks from which you’ll go on to develop more complex programs. So, having a firm understanding from the outset will help you combine these structures in a more efficient and intuitive manner. We are therefore going to spend a little time talking about these in the present and following chapters, especially selection and iteration. To begin with, here’s a short introduction to each.

Sequence

Most programs are read and executed in a linear manner. So, code written at the top will be read first, line by line. A sequenced structure is any number of ordered lines of code that are executed in a linear, one way, top to bottom manner.

Selection

Selection structures are a powerful means for changing the above behaviour. For example, there are times in a program when you want to jump to another structure but only on a certain condition. To do this, we need a selection structure, for example an if statement that says; if this variable equals a certain value then do this.

Iteration

Iteration is similar to selection because it breaks the linearity of a program. However, the particularity of this structure is that it really is dedicated to the repetition of instructions. We'll be taking a more detailed look at this structure in the next chapter.

Let’s have a look at a simple selection control structure using an if statement.

SYNTAX
keyword:if   ‹condition› ‹statement(s);›


Eg 1. js & Java
if (x > 680) {
  x = 0;
  dia = 0;
}


To demonstrate our first if statement, we are simply taking the first sketch we wrote for variables and updating the value for x and dia. If x is greater than 680 (width size for our canvas), then we assign both zero to x and dia. The if statement is based on a condition that can be expressed using various relational and/or boolean logical operators.* We can also combine these if statements to define more complex behaviours.

* Operators.
The Basic Operators

Let's develop a little on this basic control structure and do something quite different. Again, the idea of such a structure is to break the linearity of program flow and this is something we will want to do all the time, especially when developing interactive pieces. A simple yet common use of the if statement can be seen in progams that use keyboard input. Hitting keys may get you letters but we can also tell the machine to do much more when hitting a key. In this next example, we create an interaction that enables us to assign a different fill colour for our shape depending on the key we press.

Eg 2. js & Java
if (key == 'a') {
  myColour = color(0, 0, 255);
}


In the above snippet of code, the condition tests to see whether we have hit the 'a' key and if so, then we attribute the colour blue to our variable, myColour.

Code Source
Sketch: Control_01

Take A Random Walk

The if statement is a vital part of programming when it comes to coding decisions. There exists another important concept that enables us to create descisions that is worthy of mention in this chapter; randomness. Randomness is a vast subject in itself with a wealth of references and books on the matter spanning from the early throwing of dice, the search for predicting outcomes in gambling to the probability theories of Blaise Pascal & Pierre de Fermat, right up to the modern day implementation of Monte Carlo algorithms in machine learning. Its arrival in art is equally well documented, from Mozart's dice compositions to the pioneering algorithmic works of Manfred Mohr.

Indeed, the use of chance as an integral part of the artistic process became an essential part of early 20th century movements such as Dada, Surrealism and Abstract Expressionism. Notable artists were William Burroughs, Marcel Duchamp, Tristan Tzara, John Cage, to mention but a few. All shared a similar motivation for implementing chance operations as a means for shifting focus away from personal expression and liberating the creative process from the chains of control.

"Would it not be possible to describe, with linear purity of a mathematical program, “fields of events” in which random processes could occur? Thus we would have a unique dialectic between chance and program, between mathematics and hazard, between planned concepts and free acceptance of what will happen according to precise, prearranged formative patterns, which do not negate spontaneity, but give it boundaries and potential directions.."

Arte Programmata Article
Umberto Eco. Arte Programmata

Within the field of computer programming, randomness comes packaged as a function that generates pseudorandom values. It is a powerful strategy for breaking the strict uniformity of progammed work. Instead of running a program and always getting the same result, incorporating a little randomness into the process helps in creating variety and indeed surprise. There are many reasons why we may want to introduce some chance into our programs. In more complicated applications, real-world simulations rely a lot on generating unknown variables. From modelling weather systems and physical forces to generating new game play strategies, characters and game terrains. Randomness is a vital part of control structures.

Pretty much all computer languages have some built-in function for generating random values. It is a simple task of calling that function and passing in some values. Let’s have a look at this function with a basic example.

SYNTAX
random  ‹val min, val max›


Eg 1. js & Java
random (100, 320)

Code Source
Sketch: Control_02

The way this function works is that we pass in a minimum and maximum value and it will return a sequence of random values within that range. Everytime we call the function, we get a new iteration of the random sequence. This is however only the beginning. The random function may be fine for small scale and basic programs but you should dig a little more deeper into the subject if you want to get better random values. A good starting point is the Coding Math video tutorial cited below. From there, it would be a good idea to explore and learn about the noise function too which is heavily used in simulating more organic form. It's worthy to note that totally random works do not always give the most pleasant aesthetic results formally. Randomness needs some thoughtful tweaking for best results.


Concept IV: Loops >>>


Notes

Christian, Brian & Griffiths, Tom. Randomness.(pg182-204)
William Collins, 2017

Montfort, Nick et al. Randomness. (pg120-146)
MIT Press, 2013

Mlodinow, Leonard. The Drunkard's Walk: How Randomness Rules Our Lives. (pg120-146)
MIT Press, 2013

Haar, Mads. Introduction to Randomness and Random Numbers.
Random.org

Stevens, Michael. What is Randomness
YouTube 2014

Coding Math. Pseudo Random Number Generators Part I
YouTube 2014