Simulation & Design

Your Assignment

Consider you are standing at a street corner, in a city where the streets are laid out in a very regular grid pattern. At ths point you randomly choose and intersection to turn on to (left, right, forward or backward - let’s say). You walk further to another intersection and make the same random choice. Rinse, lather, repeat. When you finally stop, your winding path is some direct distance away from your starting point. This is an example of a random walk. It’s a probablistic simulation of certain statistical systems (like photons inside a star, or Brownian motion of molecules).

In n steps, how far do you expect to be from your starting point? Write a program to help answer this question.

Assignment Requirements

The requirements for this assignment are straightforward:

  1. You will need to use function(s) from the Python random module.
  2. Your main function will be used to ask for user input and loop, and perform m number of walks.
  3. You will write a function called random_walk_2d that will perform a random walk of n steps.

The distance from your starting point (origin) is a unit-less measurement, don’t worry about giving the distance any real physical distance unit.

Sample output

A simulation of 3 different walks, each taking 45 steps:

Simulation of two dimensional random walk

How man walks should I do? 3
How many steps should I take on each? 45
Average distance from start: 5.26

A simulation of 50 different walks, each taking 1000 steps:

Simulation of two dimensional random walk

How man walks should I do? 50
How many steps should I take on each? 1000
Average distance from start: 29.64

Submission Requirements

There are a few standards to adhere to when submitting your lab. Please read, and follow them carefully.

  1. Submission Filename: Submit a file named

  2. Documentation String (“docstring”): The following docstring (properly edited with your name) needs to be the very first thing in your Python program file.

    """CSC 161 Lab: Simulation & Design
    This lab...
    Fox Mulder
    Lab Section TR 2:00-3:15pm
    Fall 2015

    Read more about docstrings: Program Documentation String

  3. Comments (lines prefaced with #): Unclear parts of your code can receive point deductions, so be judicious about using comments. Do not overuse comments. Too much unnecessary commenting can complicate reading otherwise clear code!

    Read more about comments: Program Comments

  4. Python Code Style: You will need to run your Python program file through the online PEP 8 style checker found in CSC 161 Style.

  5. The “__main__, top-level script environment” check is required:

    if __name__ == '__main__':

    You can read more about this trick on the official Python website.

The Blackboard post for this lab assignment will contain the details regarding the due date and other details regarding class administration for this assignment.


The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Reasonable variations of this lab should be acceptable. In particular, we would accept programs that take input files with different formats (comma separated, space separated, one number per line, …)

Here’s a brief grading rubric:

  • Syntax errors: -30 points
  • A bad/missing docstring (i.e. no name, lab section, etc): -10 points
  • The submitted filename is wrong: -5 points
  • No Python comments used when they are needed to clarify code: -5 points
  • Code doesn’t attempt to adhere to Python coding style: -10 points
  • Your top-level script environment check is missing: -5 points
  • Missing main() function: -10 points
  • Two functions are required: random_walk_2d(), and main: -10 each/missing
  • Not using random choosing, a la Monte Carlo-style simulation.: -20 points

There may be additional criteria for grading, this is just a summary of the major rubric items.

Extra Credit

Extra credit ideas: I will allow you to add something creative and interesting to your program for extra credit. Your extra credit additions needs to be relevant to the techniques we are learning. You are not guaranteed to receive extra credit, it will be up to the lab TA to decide. Any extra credit work you do needs to be clearly commented in your code. If your extra credit code is not clearly documented using comments, you risk losing points instead of receiving bonuses.

Extra credit is added your normal submission file, do not submit a second file. Extra credit supersedes any of the above grading rubric if it conflicts.