Skip to main content

Section 24.7 Random Walks

A random walk
is a simulation technique that has been applied to solve problems in areas from physics to biology to materials science economics to who to recommend that people follow on Twitter
The basic idea is to think about a process in terms of a series of steps. These steps are done with some degree of randomness: maybe a fixed distance in a random direction, or in a fixed direction with a random distance. In a simulation, “direction” and “distance” may not mean actual physical dimensions. In a stock market simulation random walk, a step might be a price movement from one day to the next.
However, the Python turtle makes for a great visual way to do a random walk where “direction” and “distance” refer to actual physical movement. This example does a random walk where each step involves moving 10 pixels forward in one of the cardinal directions (north, south, east, west).
To generate a random direction, we are picking a random integer from 0-3 and then multiplying that by 90. This guarantees that the only numbers we can pick are 0, 90, 180, and 270. Try changing line 3 to direction = random.randrange(0, 360) and removing line 5. What is the result?

Checkpoint 24.7.1.

    What is the result of making those changes to lines 3 and 5?
  • The turtle still moves in the same way as before.
  • Did you remove line 5?
  • The turtle’s steps can go in any direction.
  • Correct.
  • The turtle goes in circles.
  • Did you try making the changes?
To simulate something like the stock market, we might want to do something like the following. Try running this program and think of the x dimension as time - the far left is day 0 and each step to the right is another day - and the y dimension as value - the price of a stock.
To take steps in this program, we track the current location of the turtle. To do a step, we increase currentX by 2 and change currentY by a random amount between -10 and 10. This will mean each step moves slightly to the right and move up/down by anywhere from -10 to 12.
One possible flaw with this program is that the change in stock prices uses a different distribution of random changes. Most days the price goes up or down by a very small amount. However, some days it climbs or drops by a lot more. Let’s figure out that logic.

Checkpoint 24.7.2.

This program should work like the one above. However, at each step, we want there to be a 2% chance that the stock change up to 40 in either direction instead of just 10. To do that, we will make a variable randValue hold a value between 1 and 100. If that is less than or equal to 2, then we will adjust currentY with a random value chosen from the larger range.
Order and indent the code blocks to build this logic. Make sure to update currentX and currentY before you do the goto .
Now that you have figured out the recipe, try using it in the program above. Does the graph look more like a stock market price history
You have attempted of activities on this page.