Wandering Turtles (Lab 7 Alternate)

Goals:

For this lab, you will simulate a world in which a bunch of turtles all are placed on a grid and allowed to wander for a while. After a period of time is up, an observer looks to see how far the turtles have wandered, and then creates a histogram of the distances.

We will use the turtle package built into Python to create a world and place turtles in it at the origin (0, 0). Each turtle will then be allowed to repeatedly choose a random direction to go, and will go 10 units in that direction. After 100 time steps, i.e., each turtle has moved 100 times, we will calculate the distance each turtle got from the origin, and then plot it.

Note: if you’re doing this on your own computer, you may need to install the matplotlib package. Follow the instructions on the Syllabus to install additional packages.

Step 1: Copy skeleton code

Your benevolent professor has graciously provided you with the skeleton code for the project. The file is called wandering_turtles.py and is found here.

You will see that 3 modules are imported at the top. The matplotlib module contains code to allow us to quickly and easily create and display a histogram.


ASIDE: Reading the turtle page on python.org can be pretty confusing, even for me. On that page, they show each function as

turtle.<function>(params)

The "turtle" in the above line is usually the name of an object called turtle, not the name of the module. So, for example, if you have made a turtle object called pen this way:

pen = turtle.Turtle()      # here turtle is the name of the module

then you would call forward() this way:

pen.forward(100)          # here, pen is a turtle object.

However, the weird thing is that the module has a hidden "default" turtle that it will use if you don't actually create a turtle object. So, if you just call

turtle.forward(100) # here, turtle is the name of the module, not a turtle object

then the code will use the hidden default object and will move that forward 100 pixels. We don't want to do that.

For this assignment you will have to create a bunch of turtles, and then move them each independently. So, call all your functions on your turtle objects, not on the default object hidden in the module.


Step 2: Function to make turtles

You see that I have created 3 constants in the code, MOVE_DISTANCE, NUM_TURTLES, and NUM_TIMESTEPS. These variables are all upper case to indicate that they are constants whose values will not be changed during the execution of the code. Also, I have commented each variable to help others understand what these variables are for.

In the # ----- Functions declared here ----- section of the code, create a function:

Name
create_turtles()
Purpose
creates turtle objects, adding them to a list that is returned.
Parameters
num: number of turtles to create
Return Value
a list of turtle objects

Here is an algorithm:

  1. Start with an empty list
  2. Repeat num times:
    1. Create a new turtle object
    2. Append the object to the list
  3. Return the list.

Notice that this follows the accumulator pattern.

In the main code area, call this function, passing in NUM_TURTLES and storing the result in a variable called all_turtles.

Step 3: Function to move turtles

Now, in your functions declaration area, create a function:

Name
move_turtles()
Purpose
moves turtle objects, each in a random "n", "s", "e", "w" direction
Parameters
turtles, distance; distance is how far to move each turtle.
Return Value
nothing

In the move_turtles() function, the code loops over every turtle in the given list. For each turtle, the code must choose a random direction – 0, 90, 180, or 270 – turn the turtle that direction, and then move the turtle forward by distance. You can use the random.choice(choices) function.

Add a call to your main code to run move_turtles(), passing in the turtle list, and the constant MOVE_DISTANCE.

Step 4: Run move_turtles() repeatedly

Now, change your main code so that there is a loop around your move_turtles() call, such that it is called NUM_TIMESTEPS times.

Step 5: Get the distances of the turtles from the origin

After we have moved turtles a random direction repeatedly, where will they end up? Some should wander far away while others end up close to the starting point. We want to graph this distribution. To do this, we need to get a list of the distances of the turtles from the origin.

To do this, create code that follows the accumulator pattern -- create an empty list, and then get the distance of each turtle, appending the result to the end of the list. Getting the distance a turtle is from the origin turns out to be very easy: look on the turtle page for help.

Write that code now. Put that code in a function that takes all_turtles as an argument.

Step 6: Graph the distances in a histogram

The plt (short for “Matplotlib”) module allows us to make graphs very easily. plt contains a nice function called hist(). I searched for plt.hist to find documentation and examples.

Add code to make a histogram from your list of distances, using 25 separate bins.

Finally, call plt.show() to make the histogram actually show up.

Step 7: Answer these questions

Answer the following questions in the Moodle quiz:

  1. Does the histogram make sense to you? Explain why it looks the way it does. (Note that this is not a Normal / Gaussian distribution, but it is in fact a closely related one.)

  2. Is there a way you could compute the data without having to watch the silly turtles move around the screen?

  3. Could you get better data by having more turtles? More time steps? More bins? More possible directions?

  4. Test some of these ideas. (You may want to copy this file to a new file and then modify that instead.) Write a comment at the bottom of your code that says what you tried and have found.

Step 8: Submit your code

Make sure you put your name and info at the top of the file, like we’ve been doing.

Submit your file to Moodle.


Grading Rubric

10 pts total:

Acknowledgments

This exercise was originally developed by Dr. Vic Norman.