Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2018

Computer Science Department

Hands-on Day

While Loops


All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.


Random Processes

As a playground for while loops, we will perform a variety of simultations of stochastic processes, making use of Python's several functions from Python's random module. This module provide a variety of functions for generating (pseduo)random numbers for a variety of common probability distributions. We will rely on a few of the most prominent functions that support a uniform distibution with various bounds:

Problems

  1. Consider a single experiment of flipping a fair coin until an outcome of "heads" and counting the number of flips that occur. Note well that randrange(2) will return either 0 or 1, with equal probability.

    Write a for loop that repeats 10,000 trials of this experiment, reporting

    Spoiler: my code
    My output:

    Average number of flips 2.0142
    Maximum number of flips 17

  2. Let's generalize the previous experiment, instead modeling an n-sided dice and rolling it until a specific outcome occurs (e.g., repeatedly rolling a 6-sided dice until seeing a two). Note well that randrange(n) will return either one of n distinct values.

    Write a for loop that repeats 10,000 trials of this experiment for n=6, reporting

    Spoiler: my code
    My output:

    Average number of rolls 6.133
    Maximum number of rolls 65

  3. Let's generalize the first experiment in a different way. Instead of continuing until we observe a heads, let's continuing until we observe k consecutive heads.

    Write a for loop that repeats 10,000 trials of this experiment for k=6, reporting

    Spoiler: my code
    My output:

    Average number of flips 125.7787
    Minimum number of flips 6
    Maximum number of flips 1081

  4. In the previous experiment, when k=6 we were effectively doing random coin flips until we observe the pattern HHHHHH among our trials. Would it affect the expected outcome if we looked for a different pattern of that length, for example HTHTHT?

    Let's conduct the experiment. Assuming that k is an even number, code the experiment that repeatedly flips coins until the most recent k flips match the pattern HTHT... for the appopriate k.

    Write a for loop that repeats 10,000 trials of this experiment for k=6, reporting

    Spoiler: my code
    My output:

    Average number of flips 83.9798
    Minimum number of flips 6
    Maximum number of flips 680

    Extra challenge: can you explain why there is such a distinct difference between the results this experiment and the previous experiment?

  5. The classic "coupon collectors problem" is modeled as follows. We presume there are n distinct coupons and that in each event we receive one of those coupons drawn uniformly at random. Our goal is to get at least one of each coupon.

    Write a for loop that repeats 1,000 trials of this experiment for n=50, reporting

    Spoiler: my code
    My output:

    Average number of picks 224.8202
    Minimum number of picks 86
    Maximum number of picks 683

  6. In the next experiment, we consider performing what is described as a random walk on a line. We consider ourselves on an infinite line. During each step we move either one unit to the left or one unit to the right, chosen with equal probability. We repeat the experiment until we "escape" by finding ourselves k units away from the starting point, for a predetermined k.

  7. Write a for loop that repeats 1,000 trials of this experiment for k=20, reporting

    Spoiler: my code My output:

    Average number of steps 410.908
    Minimum number of steps 34
    Maximum number of steps 2592


Michael Goldwasser
CSCI 1300/5001, Fall 2018
Last modified: Wednesday, 03 October 2018
Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring