Saint Louis University |
Computer Science 1300/5001
|
Computer Science Department |
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.
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:
random()
returns a floating-point
number chosen uniformly at random over the interval
uniform(a,b)
returns a floating-point
number chosen uniformly at random over the interval
randrange(stop)
randrange(start,stop)
randrange(start,stop,step)
returns an integer chosen uniformly at random over the
notion of the corresponding Python range. For example,
randrange(5) will choose uniformly from the values
randint(a,b)
returns an integer chosen uniformly at random over the
closed interval
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
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
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
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?
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
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.
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