Objectives

Working with loops and arrays.

To encourage incremental development, we are requiring that in your solutions document you show your results for each part!

Repetition in random patterns

If you flip a coin repeatedly, occasionally you will get long runs of the same side. If you do enough flips, you will find some very long runs. You are going to perform a simulation and analyze the results. When your program is finished it will produce a result similar to this:

Welcome to the coin flip analyzer.
How many flips? 50
H TTT HH T H TT HHHHH T HHH TTT HHHHH T HH TTT H T H TTT H TTTTT HHHHH...
[run length] = frequency (percent)
[1] = 9 (45.0%)
[2] = 3 (15.0%)
[3] = 5 (25.0%)
[4] = 0 (0.0%)
[5] = 3 (15.0%)

Part 1 (5 points) Random Sequence of coin flips

To begin, write the code to ask the user how many flips to perform, and then use Random to get that many random values. In my code I used the line: int coin = rand.nextInt(2);
Print either H or T for each (Heads or Tails). This is the first three lines of the output shown above except that you don't yet need to print the spaces between runs or the ... on the end.

In your solutions document show your  output.

Part 2 (5 points) Spaces between runs

Now add code to print a space between each run as shown in the example. To do this, you will need to keep track of the previous coin flip to test if the previous run has ended. In my code I used the variable: prevCoin. Think about the end cases: there should not be a space before the first flip nor should there be one after the last flip. Print "..." at the end to indicate that the the last run was not actually completed.

In your solutions document show your output.

Part 3 (5 points) Count the run lengths

Next add code to count the run lengths and print the run length values immediately following each run as shown below. Note that the last incomplete run does not have a length printed. (Later we will remove the printing, but for now it will allow us to check  our results.)

H1 TTT3 HH2 T1 H1 TT2 HHHHH5 T1 HHH3 TTT3 HHHHH5 T1 HH2 TTT3 H1 T1 H1 TTT3 H1 TTTTT5 HHHHH...

In your solutions document show your output.

Part 4 (5 points) Tabulating statistics

Make an array (size 10 for now) and count how many times each run length occurs. (You won't be using index 0 in this array, just indices 1 to 9.)

Also calculate the total number of runs. You will need this to calculate the percentages of each run length.

Finally print a table similar to the example above, except print all indices 1 to 9.

In your solutions document show your output.

Part 5 (5 points) Used portion of the table

Calculate the index of the last non-zero value in your array, and then modify your table printing to only print up to that point (as in the example).

Run this a bunch of times and make sure everything is correct.

In your solutions document show 1 example output.

Part 6 (5 points) The BIG test!

Now that it all works correctly, you are going to do a really big test of 1 billion coin flips.

First modify the printing to get rid of the run length printing, and to only print the first 50 flips and their separating spaces. It should now look like the example at the top even when there are more then 50 flips.

You will also need a larger array because in a billion flips there will be some large runs. Try to guess how long the largest run will be, and make your array a little larger than what you think you will need. If you guess too small, you will get an ArrayIndexOutOfBoundsException. If you guess too large, you will just waste a little memory.

This took my computer about 10 seconds to run. If yours is a lot slower, look for inefficiencies in your code such as some printing that continued after 50 flips.

Do you notice anything interesting about the run length percentages?

In your solutions document show your output and your final code.

What to turn in

Turn in your output for each of the 6 parts and your final code.