Random Walk

By Monty, 18th April 2015

RandomWalk1A colourful random walk. The basic idea is very simple: choose a random heading in the range 0:360 degrees. Step in that direction. Choose another random heading & step again. Do this 2,000 times. As a bonus, we go through all the colours of the rainbow (or something similar) from beginning to end.

Dealing with colours complicates this program (or any other). The 2 main colour models in computer graphics are RGB and HSV. Turtle and many other graphics libraries accept RGB but not HSV. However, HSV is most convenient for cycling through rainbow colours (I say ‘rainbow’ but there are some hues in HSV that aren’t in real rainbows. Note for example the brownish colour in the picture. Never mind). Just start the hue at 0 and increment it at each step, up to 1 (or 100 in some systems), then convert to RGB using the colorsys library.

#   Random Walk, in glorious Technicolour
#   https://docs.python.org/3/library/turtle.html
#   Authour: Alan Richmond, Python3.codes

from turtle import *
from random import randint
from colorsys import hsv_to_rgb

step=30                 # length of each step
nsteps=2000             # number of steps
hinc=1.0/nsteps         # hue increment
width(2)                # width of line

(w,h)=screensize()      # boundaries of walk
speed('fastest')
colormode(1.0)          # colours 0:1 instead of 0:255
bgcolor('black')        # black background
hue=0.0
for i in range(nsteps):
    setheading(randint(0,359))
    #   https://docs.python.org/2/library/colorsys.html
    color(hsv_to_rgb(hue, 1.0, 1.0))  # pen colour in RGB
    hue+=hinc                           # change colour
    forward(step)                       # step along!
    (x,y)=pos()                         # where are we?
    if abs(x)>w or abs(y)>h:            # if at boundary
        backward(step)                      # step back
done()
Wikipedia:

A random walk is a mathematical object, known as a stochastic or random process, that describes a path that consists of a succession of random steps on some mathematical space such as the integers. 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 approximated by random walk models, even though they may not be truly random in reality. As illustrated by those examples, random walks have applications to many scientific fields including ecology, psychology, computer science, physics, chemistry, biology as well as economics. Random walks explain the observed behaviors of many processes in these fields, and thus serve as a fundamental model for the recorded stochastic activity. As a more mathematical application, the value of pi can be approximated by the usage of random walk in agent-based modelling environment. The term random walk was first introduced by Karl Pearson in 1905.

Various types of random walks are of interest, which can differ in several ways. The term itself most often refers to a special category of Markov chains or Markov processes, but many time-dependent processes are referred to as random walks, with a modifier indicating their specific properties. Random walks (Markov or not) can also take place on a variety of spaces: commonly studied ones include graphs, others on the integers or the real line, in the plane or in higher-dimensional vector spaces, on curved surfaces or higher-dimensional Riemannian manifolds, and also on groups finite, finitely generated or Lie. The time parameter can also be manipulated. In the simplest context the walk is in discrete time, that is a sequence of random variables (X
t
) = (X
1
, X
2
,...)
indexed by the natural numbers. However, it is also possible to define random walks which take their steps at random times, and in that case the position X
t
has to be defined for all times t ∈ [0,+∞). Specific cases or limits of random walks include the Lévy flight and diffusion models such as Brownian motion.

Random walks are a fundamental topic in discussions of Markov processes. Their mathematical study has been extensive. Several properties, including dispersal distributions, first-passage or hitting times, encounter rates, recurrence or transience, have been introduced to quantify their behaviour.

What do you think?

Leave a Reply

%d bloggers like this: