### Search

 Search WWW Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway,  consider using this link.

We receive a few cents from each purchase.  Thanks

### Support DFF - Donate

If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

(Hint: If you can wait, current year calendars are usually on sale in January.)

### Contact

 Search DelphiForFun.org only

### Problem Description

This program explores the phenomena of "runs" when flipping a coins.  A run is a string of consecutive flips with identical results.  Long runs seem to occur more often than we think they should.

### Background & Techniques

Natalie Angier in her book "The Canon" has a chapter about probabilities which describes an experiment conducted by a math professor with new classes. Each student flips a coin 100 times and records the results with the instructor out of the room.  The hook is that half of the class actually flips a coin, the other half "flips" mentally, i.e. just records what they imagine the results of the next flip might be. The instructor returns to the room and sorts the results according to whether they represent real or imagined tosses.

The trick works because most minds do not appreciate the occurrence of "runs", sequences of tosses with the same result. Almost every set of 100 tosses will have have one or more sets of 5 or more Heads or Tails in a row. (The expected number of such strings is slightly more than 3.0.) Very few of the human-imagined results will have even one such string.  This program verifies the result by mathematically flipping coins and comparing experimental with expected results for a range of "same result" string lengths.

Two buttons represent alternative experimental techniques.  Button1 simulates the experiment described above, throw 100 coins per trial for the number of trials specified and record number of runs of length N.

The 2nd button reports runs of length N per 100 trials if only N are thrown at a time. Until you think about it, it's surprising that the second method produces results 4 times larger than Method 1. This is because when throwing N coins per trial and counting runs of length N, the preceding and following coins do not matter. If we throw 100 coins looking for runs of length N, the coins preceding and following a "success" must show the

opposite side. So runs of length 3 Heads require a "run" of length 5 with value THHHT which have a probability of 1/32 compared to 1/8 for runs of length 3 when only 3 coins are thrown.

#### Notes for Programmers

Nothing too hard except figuring out why flipping a coin 100 times and counting runs of length N has only 1/4 the occurrences of runs of length N  compared to N coins thrown 100 times and length N runs counted.     Procedure "Trial" simulates results if NbrCoins are thrown consecutively and counts the number of runs for each length from 1 to Runlength.  Counts of number of Heads runs and Tails runs are returned in separate arrays.

To simulate the original experiment, Trial(100, 10, HCount, TCount) is called Nbrtrials times, each call representing one trial.  Nbrtrials is set from the value of the Radio Group box, Radiogroup1..  The totals from each trial for all 10 run lengths  are totaled in arrays  HeadRuns and TailRuns.   These totals, divided bynbrtrial are reported as observed values.

For the 2nd button, simulating runs of length N observed when only N coins are flipped in each trial, an outer loop for each run length 1 through 10 contains an inner loop calling Tria( i, i, HCount, TCount) Nbrtrials times and summing  only Hcount[ i ] and TCount[ i ] for i= 1 to 10.