Probabilities & Random Walks
Randomness
We need randomness to model the nature and variety of the physical world. In Python we can create a random number from within a range using random(min,max)
for example: random(-10,50)
.
Randomness doesn’t often occur normally distributed. So we can use other random ranges to simulate different normal ranges. For example randomGaussian()
creates numbers with a mean of 0 and standard deviation of 1. Gaussian distribution represents a bell curve distribution. randomSeed()
allows you to return the same psuedo-random number each time with a seed.
Perlin noise (noise
) often used to simulate natural phenomena, for example: procedural terrain (like Minecraft), fire effects, water, and clouds. In Processing noiseSeed()
allows you to select the same pseudo-random numbers each time using a seed, further noiseDetail()
allows for varied levels of noise resolution. Two fun facts: Did you know Perlin noise was actually invented to create realistic textures for the 1980s Tron by Ken Perlin. Did you also know that Tron Legacy was directed by Joseph Kosinski, former GSAPP student who developed techniques in 3ds Max in early versions of Josh Uhl’s ADR course.
noiseVal = 0
noiseScale = 0.05size(500,500)for y in range(height):
for x in range(width / 2):
noiseDetail(3, 0.5)
noiseVal = noise(
x * noiseScale, y * noiseScale)
stroke(noiseVal * 255)
point(x, y)
noiseDetail(8, 0.65)
noiseVal = noise((x + width/2) * noiseScale,
y * noiseScale)
stroke(noiseVal * 255)
point(x + width/2, y)
To learn more about creating procedurally generated terrain using noise, check out this video!
Random Walks
Random walks represent the movement of a point or an agent in a random set of a directions. At each step the point or agent moves on new random direction. Random walks are used to test the statistical likelihood for change or movement — in economics, biology, and many other fields. In mapping and simulation, random walkers can help decipher how far away something is, given many possible ways of getting there and a statistical likelihood of how far something will veer from its origin or where it will end up given an amount of steps . To learn more about random walkers in Python watch this video.
Random walks return to the same location many times, thus oversampling the same spot. By jumping a further distance of steps, oversampling can be avoided.
import random
def setup():
global x, y
size(500,500)
stroke(0)x = width/2
y = height/2
def draw():
global x, y
for i in range(1):
step = random.choice(['N', 'S', 'E', 'W'])
if step =='N':
y=y+1
elif step == 'S':
y=y-1
elif step == 'E':
x=x+1
else:
x=x-1
print (x,y)
point (x,y)
Maze makers and maze solvers like this one can be used to determine efficient routing. More about maze solvers here.
In 1736 the mathematician Leonhard Euler pondered about the nature of distance and travel in the old city of Königsberg, Germany. The city (left diagram below) had two islands connected by 7 bridges. Which route would allow someone to travel across all 7 bridges without crossing a bridge twice? The answer: its not possible. During this pondering he invented a simplified way to represent this problem and similar ones by drawing simple graph diagrams of nodes to represent destinations and connecting lines, “edges”, representing the number of connections that are possible. This allowed for a simplified view that could show any set of nodes would need to be paired for a traveler to not cross twice. This creation of Graph Theory, has gone on to be useful not only in mapping, routing and connection problems, but is also useful in graphing the “distance” within networks elsewhere. For example Wikipedia pages can be graphed by number of connecting links to relevant words.
Movement
Challenge: Create a moving agent that speeds up every time it comes into contact with the edge of the border. Create a agent that steps moves with a random jitter.
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Example 1-1: Bouncing Ball, no vectorsx = 100
y = 100
xspeed = 2.5
yspeed = 2def setup():
size(800, 200)
smooth()def draw():
background(255)
# Add the current speed to the location.
global x, y, xspeed, yspeed
x = x + xspeed
y = y + yspeed
if (x > width) or (x < 0):
xspeed = xspeed * -1
if (y > height) or (y < 0):
yspeed = yspeed * -1
# Display circle at x location
stroke(0)
strokeWeight(2)
fill(127)
ellipse(x, y, 48, 48)
Random acceleration through a class:
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.comdef setup():
size(640, 360)
global mover
mover = Mover()def draw():
background(255)
mover.update()
mover.checkEdges()
mover.display()class Mover(object):def __init__(self):
self.location = PVector(width / 2, height / 2)
self.velocity = PVector(0, 0)
self.topspeed = 6def update(self):
self.acceleration = PVector.random2D()
self.acceleration.mult(random(2))
self.velocity.add(self.acceleration)
self.velocity.limit(self.topspeed)
self.location.add(self.velocity)def display(self):
stroke(0)
strokeWeight(2)
fill(127)
ellipse(self.location.x, self.location.y, 48, 48)def checkEdges(self):
if self.location.x > width:
self.location.x = 0
elif self.location.x < 0:
self.location.x = widthif self.location.y > height:
self.location.y = 0
elif self.location.y < 0:
self.location.y = height