72
3 Loops and Branching
Getting indices right
To implement the traversing of arrays with loops and indices, is often
challenging to get right. You need to understand the start, stop and step length
values for the loop variable, and also how the loop variable (possibly) enters
expressions inside the loop. At the same time, however, it is something that
programmers do often, so it is important to develop the right skills on these
matters.
You are encouraged to test your understanding of the search procedure
in ball_max_height.py by doing Exercise 3.9. That exercise will ask you
to compare what you get “by hand” to printouts from the code. It is of
fundamental importance to get this procedure as an established habit of yours,
so do the exercise right now!
3.3.4 Example: Random Walk in Two Dimensions
We will now turn to an example which represents the core of so-called random walk
algorithms. These are used in many branches of science and engineering, including
such different fields as materials manufacturing and brain research.
The procedure we will consider, is to walk a series of equally sized steps, and
for each of those steps, there should be the same probability of going to the north
(N), east (E), south (S), or west (W). No other directions are legal. How can we
implement such an action in a computer program?
To prepare our minds for the coding, it might be useful to first reflect upon how
this could be done for real. One way, is to use a deck of cards, letting the four suits
correspond to the four directions: clubs to N, diamonds to E, hearts to S, and spades
to W, for instance. We draw a card, perform the corresponding move, and repeat
the process a large number of times. The resulting path mimics, e.g., a typical path
followed by a diffusing molecule.
In a computer program, we can not draw cards, but we can draw random numbers.
So, we may use a loop to repeatedly draw a random number, and depending on the
number, we update the coordinates of our location. There are many ways to draw
random numbers and “translate” them into our four directions, and the technical
details will typically depend on the programming language. However, our technique
here is universal: we draw a random number from the interval [0, 1) and let [0, 0.25)
correspond to N, [0.25, 0.5) to E, [0.5, 0.75) to S, and [0.75, 1) to W. We decide
to simulate 1000 steps, each of length 1 (e.g., meter), starting from Origo in our
coordinate system. To enable plotting our path, we use two arrays for storing the
coordinate history, one for the x-coordinates and one for the corresponding ycoordinates.
The suggested code random_walk_2D.py then reads
import random
import numpy as np
import matplotlib.pyplot as plt
N = 1000
# number of steps
3 Loops and Branching
Getting indices right
To implement the traversing of arrays with loops and indices, is often
challenging to get right. You need to understand the start, stop and step length
values for the loop variable, and also how the loop variable (possibly) enters
expressions inside the loop. At the same time, however, it is something that
programmers do often, so it is important to develop the right skills on these
matters.
You are encouraged to test your understanding of the search procedure
in ball_max_height.py by doing Exercise 3.9. That exercise will ask you
to compare what you get “by hand” to printouts from the code. It is of
fundamental importance to get this procedure as an established habit of yours,
so do the exercise right now!
3.3.4 Example: Random Walk in Two Dimensions
We will now turn to an example which represents the core of so-called random walk
algorithms. These are used in many branches of science and engineering, including
such different fields as materials manufacturing and brain research.
The procedure we will consider, is to walk a series of equally sized steps, and
for each of those steps, there should be the same probability of going to the north
(N), east (E), south (S), or west (W). No other directions are legal. How can we
implement such an action in a computer program?
To prepare our minds for the coding, it might be useful to first reflect upon how
this could be done for real. One way, is to use a deck of cards, letting the four suits
correspond to the four directions: clubs to N, diamonds to E, hearts to S, and spades
to W, for instance. We draw a card, perform the corresponding move, and repeat
the process a large number of times. The resulting path mimics, e.g., a typical path
followed by a diffusing molecule.
In a computer program, we can not draw cards, but we can draw random numbers.
So, we may use a loop to repeatedly draw a random number, and depending on the
number, we update the coordinates of our location. There are many ways to draw
random numbers and “translate” them into our four directions, and the technical
details will typically depend on the programming language. However, our technique
here is universal: we draw a random number from the interval [0, 1) and let [0, 0.25)
correspond to N, [0.25, 0.5) to E, [0.5, 0.75) to S, and [0.75, 1) to W. We decide
to simulate 1000 steps, each of length 1 (e.g., meter), starting from Origo in our
coordinate system. To enable plotting our path, we use two arrays for storing the
coordinate history, one for the x-coordinates and one for the corresponding ycoordinates.
The suggested code random_walk_2D.py then reads
import random
import numpy as np
import matplotlib.pyplot as plt
N = 1000
# number of steps
