Tossing a (biased or unbiased) coin


Below is a program that allows you to choose a bias towards heads for a coin toss, and then simulates tosses until you stop the program.


You may ask the program at any time, without stopping it, what the current outcomes of heads and of tails are, together with their frequencies.  Here is an example:


 

 

 

Explanation of the code:


PROGRAM:COINTOSS

 

:Disp “CHANCE OF HEADS?”

 

:Input H

Enter a decimal between 0 and 1, which is the probability of getting heads, and press ENTER.

:{0,0}→LF

List F holds two items. LF(1) holds the number of tails, and LF(2) holds the number of heads.  Thus far there are no tosses.

:While 1

While 1 performs the commands below until you stop the program by pressing ON ENTER.

:(rand<H)+1→R

rand<H is either 1 or 0, and when 1 is added to it, R becomes either 2 (the outcome is heads) or 1(the outcome is tails).

:LF(R)+1→LF(R)

Increase the number of heads or the number of tails by one.

:If getkey>0

getkey is a variable which is assigned a number when you press a key. If you press any key, getkey is greater than 0.

:Then

 

:Disp sum(LF),

LF,E2round(LF/sum(LF),3)

The calculator will display the current total number of tosses, sum(LF), the current total number of occurrences of tails, the current total number of heads, and the percentage frequencies of each, rounded to tenths.

End

End of If loop.

End

End of While loop.


Let’s put this program in our calculator and play with it for a few different values for H, the probability of getting heads, and run it for different lengths of time.

After we stop the program, we can look at:
H  the probability of getting heads, which we entered
R   either 0 or 1; it gives the last toss, either tails or heads
LF  the final number of tails and the final number of heads







Webpage Maintained by Owen Ramsey
Lesson Index