Coin Flip - B
16
16
In this lesson you will use a modify the coin flip app from Coin Flip A in order to run and experiment of many simulations of many flips.
The CoinFlipExperiment
app will let you conduct an experiment aimed at determining how 'good' App Inventor’s Pseudo Random Number Generator (PRNG) is. The app will let you 'flip a coin' N times and display the results.
Later in this lesson you will run the app several times, recording and tallying the results and calculating the percentage of heads. The expectation is that as N gets large, the percentage of heads should approach 50%.
model
- an abstraction that provides a simplified representation of some complex object or phenomenon.fair coin
- A coin that, when flipped, would come up heads 50% of the time over a large number of coin flips.hypothesis
- An explanation that can be tested by experimentation.CoinFlipExperiment
before continuing with this part of the tutorial. Let's revise the UI so it looks like this:
HorizontalArrangment
at the very top of the screen. Then add the coin Image
to the arrangement followed by a Label named LabelTitle
. Set the image's width and height to 50 pixels and the LabelTitle
's font size to 30. The LabelTitle
should whould have text: "Coin Flip Experiment." HorizontalArrangement
just below the first one and then add a Label
and a TextBox
to it. This Label should be named LabelN
and given the title "N:
", and the TextBox should be named TextBoxN
and have its NumbersOnly
property set to true
(checked). Button1
to ButtonGo
and its label to "Run Experiment" and add it to the right hand side of the HorizontalArrangement
. Add another label named LabelResults
below the horizontal arrangements and label it "Results". Set its font to 24. This is where we will display the results of the experiment.
Clearly we're going to need some additional variables to manage the experiment. Add the following variables to the app:
N
- initialize N
to 0. This will be the number of coin flips based on the user's input in the TextBox
.nHeads
- initialized to 0. We'll use this variable to count the number of heads in the experiment. What kind of problem are we using the loop to solve? Basically, if the user inputs 100 we want to flip the coin 100 times and on each flip we want to test whether it comes up heads or tails. This is a counting problem: i.e, we need to count the coin flips, starting at 1 and stopping at 100. More generally, we need to count from 1 to N (whatever the user input). The best loop for this kind of problem is App Inventor's For each number from __ to __ by __
loop, where the __
's are number place holders.
The loop will repeatedly perform whatever operations we put inside its do slot.
In our case, we want to replace the 5
from the image on the left to the variable N
, the total number of coin flips to perform, like the image on the right.
When you're coding a loop, it is necessary to perform some initialization steps. In this case we need to set the value of N
, which will control when the loop will stop. This variable will be initialized by taking the value the user inputs into the TextBox
. And we need to initialize nHeads
to 0: because, as we're going to be counting the number of heads we get, we want to start counting at 0.
What needs to be done in the body of the loop: i.e., in the do
slot? We need to "flip" the coin and then count whether it came out heads or tails. We already know how to do this using the random integer
block for the coin flip and an if/else
for checking whether it's heads or tails. In this case, however, rather than displaying a heads or tails image, we're going to add 1 to the nHeads
variable.
When the loop finishes, we will report the results in the LabelResults
. Here's the format we will use: Heads: 52 Tails: 48
We can do this by using string concatenation (the join
block) as seen.
Notice that the number of tails is calculated by simply subtracting the nHeads
from N
.
Formatting Alert! Notice how there are spaces after the colons and before Tails. These need to be added inside the text "" blocks in the correct spots, otherwise the output will not look good.
All of these steps are combined into a single algorithm in the ButtonGo.Click
event handler, as shown below in both App Inventor and pseudocode. (NOTE that College Board style pseudocode does not contain a For each number from 1 to N
loop. Instead we use its REPEAT N times
loop, which is equivalent in this case.)
If you have coded the app as shown here, then whenever ButtonGo
the app will perform one trial of the experiment consisting of N
simulated coin flips. Try varying the value for N
: you can try 100, or 1000, or 10,000 or any other value. NOTE: Be careful with very large values of N
, that might take a long time causing your device to become unresponsive, which may even generate a runtime error message from Android.
Many times, when users run apps or programs, they do not always stay within the parameters of the program. In this case, users might accidentally enter words instead of numbers, or enter numbers so large that the program will crash. Both of these instances are unacceptable.
In order to ensure your app works as intended, the last thing we should do, or maybe the first thing if we designed the app with input checking before now: is to check the user input and only run the loop if the user input a valid number:
Seen is the finished code with a conditional if statement that does this check:
If you had a fair coin and you flipped it many, many times: maybe a million times then if it were truly fair, you would expect it to come up "Heads" half the time. That's why we say for any coin flip, it has a 50:50 chance of coming up heads.
App Inventor's random integer block uses its PRNG to generate a random sequence of integers. In our app, the sequence is between 1
and 2
inclusive. So, if the PRNG is good, it should generate a 1
half the time and a 2
half the time. And this, in turn, should let our Coin Flip app be a good model of flipping a coin.
Our hypothesis is that App Inventor's random integer block
is a good approximation of the process of randomly generating a 1
half the time and a 2
half the time.
If you were testing that a particular coin was 'fair', you would flip it lots of times and record the number of heads and tails. Their ratio should come out 50:50. But you have to do a lot of flips.
So, to test our hypothesis about App Inventor's random integer block, we have to perform a simulated "coin flip" lots of times. To help with this, we will use the Coin Flip Experiment
app, which will let us repeatedly 'flip' a coin. The app uses an algorithm that uses the random integer
block. If the random integer
block is a good approximation of randomness, we would expect that when it is used to model the process of flipping a coin, it would make the odds of getting a "Heads" or "Tails" 50:50.
For our hypothesis to be true, the ratio between "Heads" and "Tails" in the app should approximate 50:50 as the number of coin flips gets large. The more coin flips we perform, the closer our ratio should be to 50:50.
If the ratio does approach 50:50, that would validate our hypothesis. If it does not, that would prove that our hypothesis is invalid.
1. Repeatedly run the app and record the number of heads and tails received in each run.
2. Tally your results into the provided spreadsheet for each trial.
3. Use spreadsheet functions to calculate the % tails for each trial.
E2
, first type "=1-
", then click on cell D2
before pressing enter. This will automatically calculate the % Tails.E2
, then click and drag the blue box in the bottom right corner down to all the cells in column E.