Random Walker

Information

A random walk is a mathematical formalization of a path that consists of a succession of random steps. For example, the path traced by a molecule as it travels in a liquid or a gas, the search path of a foraging animal, the price of a fluctuating stock and the financial status of a gambler can all be modeled as random walks, although they may not be truly random in reality. [ en.wikipedia.org/wiki/Random_walk ]

Zufall Der Zufall in der Informatik ist eng Verbunden mit der Wahrscheinlichkeitsrechnung, denn Computer können nicht wirklich zufällig arbeiten, sondern jeglicher Zufall wird über eine Algorithmus simuliert. In Processing geschieht die Erzeugung von Zufallszahlen über die Funktion »random (min, max)«, wobei eine zufällige Zahl zurück gegeben wird, welche sich innerhalb des angegebenen Minimal- und Maximalwertes befindet.

Random Walk Der Random Walk, zu deutsch Zufallsbewegung oder auch Irrfahrt, basiert auf einem stochastischen Prozess. Er findet Verwendung in der Herleitung von Wahrscheinlichkeitsverteilungen. Unter Anderem kann auf diese Weise der zeitliche Verlauf beziehungsweise die Entwicklung von Marktpreisen nachvollzogen werden.

Grafisch kann dieser Vorgang als wandernde Linie beschrieben werden, welche an einem zufälligen Punkt auf einer zweidimensionalen Fläche beginnt. Bei jedem Durchgang wird die Linie um einen Punkt erweitert. Dabei wird zufällig entschieden, ob die Linie nach oben, nach unten, rechts oder links weiter geht.

Zusammenfassend lässt sich der Random Walk durch folgende Eigenschaften definieren: Alle Einzelentscheidungen sind autonom. Jeder Schritt hängt ausschließlich von der aktuellen Position ab und nicht von den vorher besuchten Plätzen. Jeder Teilschritt wird zu einem neuen Ausgangspunkt. Daher können Plätze mehrmals aufgesucht werden und der daraus entstehende Pfad weist Kreuzungen auf.

Definitionen

Variablen Variablen werden dazu benutzt um Daten festzuhalten. In Processing und den meisten anderen Programmiersprachen gibt es folgende Datentypen: int (Integer) für Ganzzahlen, float für Gleitkommazahlen, boolean für wahr / falsch, char für ein einzelnes Zeichen (z. B. ein Buchstabe) und color für Farben. Bei allen diesen Datentypen spricht man von »Primitives«, also einfachen Datentypen. Darüber hinaus gibt es noch »Composites«, zusammengesetze Datentypen. Dazu gehören Strings für Zeichenketten (z. B. Wörter), Arrays, ArrayLists und Objects. Unabhängig vom Datentyp erfolgt bei dem Primitives die Deklaration und Wertzuweisung immer gleich:

Typ name = Wert;
int iNum = 12; (Beispiel mit »iNum« als Variablenname)

Jeder Datentyp »verbraucht« eine bestimmte Menge an Speicher. Um beispielsweise ein Integer zu speichern werden 32 bits benötigt, bei einem String ist es schon doppelt so viel. Gerade für aufwendige Programmierungen und Programme, die in Echtzeit laufen sollen, muss der Speicherverbrauch bewusst eingesetzt werden und möglichst immer auf den kleinsten Datenspeicher zurück gegriffen werden.

Funktionen und Methoden Funktionen sind dazu da innerhalb des Programmes Teilprobleme zu lösen oder Daten zu verarbeiten. Man kann zwischen verschiedene Typen von Funktionen unterscheiden. Es gibt zum Beispiel Funktion mit und ohne Rückgabewert und mit oder ohne übergebene Paramter.

Ein einfaches Beispiel zur Berechnung einer Summe könnte so aussehen:

int summe (int a, int b)
{
   return (a+b);
}

»int« (Ganzzahl) ist der Rückgabewert, dass heißt das an der Stellen des Programmes, an dem die Funktion aufgerufen wird, die Summe aus den beiden übergebenen Paramtern »a« und »b« berechnet wird. »summe« ist der Funktionsname, durch welchen die Funktion wie im folgenden Beispiel aufgerufen werden kann:

int x = summe (2, 4);

Klassen und Objekte Klassen sind Sammlungen von Attributen und Methoden. Sie dienen dazu, den Code in komplexen Programmen zu organisieren und zu strukturieren. Oder anders gesagt, jede Klasse ist dafür da, ein Problem zu lösen oder eine bestimmte Aufgabe zu erfüllen. Instanzen von Klassen werden als Objekte bezeichnet. Ein beliebtes Veranschaungsbeispiel für die Erklärung von Klassen ist der Mensch. Sabine bespielsweise ist eine Instanz der Klasse Mensch. Sie ist blond, hat grüne Augen und ist 1.78 m groß. Marco hingegen hat schwarze Haare, braune Augen und ist 1,82 m groß. Beide gehören sie der Klasse Mensch an, denn sie haben die gleichen Attribute: Haarfarbe, Augenfarbe, Größe. Werden Klassen verwendet, so wird dies als objektorientierte Programmierung, oder kurz OOP, bezeichnet.

Short References

Downloads (German) Variablen und Datentypen
Bedingungen
Funktionen
Random Walk

Progam Controls frameRate (value);
println (value); (prints the value into console)
noLoop(); (draw() will run just once)
loop(); (draw() will run continuously)
redraw(); (run draw once - needed with noLoop() )

Event Functions void keyPressed() { /* Content of keyPressed */ }
void mousePressed() { /* Content of mousePressed */ }

Primitives int iNum = 20; (Integers)
float fNum = 2.45; (floating point number)
boolean isTrue = true; (true or false)
boolean isFalse = false;

Casting int iNum = 45;
float fNum = 3.45;

float newFloat = iNum; (Automatic casting)
float anotherNewFloat = int (fNum); (Explicit casting)
float lastNewFloat = (int) fNum; (Explicit casting)

Calling Variables int d = 5;
ellipse ( 50, 60, d, d);

System Variables width, height (int) (window size)
mouseX, mouseY (int)(mouse position)
pmouseX, pmouseY (int) (mouse position of last frame)
frameRate (float) (current framerate)
keyPressed (boolean)
mousePressed (boolean)

Mathematics int iNum = 5;
int jNum = 10;
int kNum = iNum + jNum; (kNum = 15)
kNum = kNum + 4; (kNum = 19)
kNum++; (increasing by one)
kNum--; (decreasing by one)

float fNum = 2.5;
fNum = fNum + iNum; (same as fNum += iNum)
fNum = fNum - iNum; (same as fNum -= iNum)
fNum = fNum * iNum; (same as fNum *= iNum)
fNum = fNum / iNum; (same as fNum /= iNum)

Random float rNum = random (min, max);

Conditionals if (condition1)
{
   /* if condition1 is true this part of code will run */
}
else if (condition2)
{
   /* if condition2 is true this part of code will run */
}
else
{
   /* if condition1 and condition2 are false this part of code will run */
}

Define a function void functionName ()
{
   /* Content of functionName */
}

Calling a function funtionName();

Exercises

Loops

Your browser does not support the canvas tag.

Get the Code: variables_a.pde

Click to start over. noLoop() and redraw();

Your browser does not support the canvas tag.

Get the Code: variables_b.pde

Looping example with random generated position and slow framerate.

Background & Transparency

Your browser does not support the canvas tag.

Get the Code: background_a.pde

background() is written into the draw function so it is drawn every frame.The ellipse's position parameters are based on the system variables mouseX and mouseY

Your browser does not support the canvas tag.

Get the Code: background_b.pde

background() is written in the setup() function, so it is just drawn once. The tracing effect is generated with a transparent layer (rectangle with the size of the window).

Movement

Your browser does not support the canvas tag.

Get the Code: basicMovement.pde

The ellipse's position is based on two variables: float x; and float y; Each frame the x value is increased by one. This causes, that the ellipse moves from left to right (because the x values is getting bigger and bigger).

Your browser does not support the canvas tag.

Get the Code: basicMovement2.pde

The diameter of the ellipse changes over time.

Random Walk Simple

Your browser does not support the canvas tag.

Get the Code: walker_simple.pde

Click to start over. Random Walker which makes random sized steps.

Your browser does not support the canvas tag.

Get the Code: walker_simple_2.pde

Click to start over. Random Walker with random sized steps (and size of ellipse). It also saves the last position of each step in two new varialbes float lastX; and float lastY; With this a continuously line can be drawn: Current position and last position are connected with a line (x, y, lastX, lastY);

Random Walker Functions and Class

Your browser does not support the canvas tag.

Get the Code: walker_01.pde

Click to start over. Random Walker which makes random sized steps. Code is divided into seperate functions.

Your browser does not support the canvas tag.

Get the Code: walker_02.pde

Click to start over. Random Walker class and two instances of it.