4.5. Coin Flip Simulation Tutorial¶
Time Estimate: 45 minutes
4.5.1. Introduction and Goals¶
Coin Flip is an app that simulates a coin flip. In fact, because it uses App Inventor's random number generator, it may actually be fairer than a real coin flip. That is, it may come closer than a real coin flip to producing "heads" 50% of the time. This tutorial has two parts. In the first part we will build a simple app that simulates a coin flip. The coin will be represented by a global variable. Heads will be represented by 1 and tails by 2, and flipping the coin will be a matter of randomly assigning 1 or 2 to that variable. The result will be displayed as an image depicting either the heads or tails of a coin. In part two, we will extend the app to perform the coin flip many times in order to experiment with the idea of "randomness" or "fairness". If you flip a real coin lots of times, it should come up heads close to 50% of the time. If you simulate a coin flip lots of times, it should also come up heads close to 50% of the time  unless the "randomness" that's built into App Inventor is not a very good model of true randomness. We'll explore the idea of randomness later in this Unit. 

Learning Objectives: I will learn to
Language Objectives: I will be able to

4.5.2. Learning Activities¶
A Short Experiment
Before getting started on the Coin Flip app, try this simple experiment:
 If you flip a fair coin 20 times  any type of coin will do  how many heads would you expect to get? Write down your answer.
 Now flip the coin 20 times and count the number of heads. Write down the count.
 Did the count match your explanation? Based on this experiment, could you conclude that your coin is fair or biased (not fair).
If you perform this experiment, heads will often come up 10 times, but not always, even though, supposedly, the probability of getting a head on a fair coin toss is 50%.
The problem with this experiment is you didn't perform enough trials to draw any conclusion about the hypothesis that this is a fair coin.
The Random Block
In App Inventor, we will use the random block to get pseudorandom numbers. In the AP CSP exam, the function RANDOM(1,3) is used to return a random number from 1 to 3 (including 1, 2, or 3).AP Pseudocode  App Inventor 

x = RANDOM(1,3) 
Tutorial Part I: Simulating a Coin Flip
To get started, open App Inventor with the Coin Flip Media Only template. If the template does not open, download the .aia file, go to App Inventor and do File/Import and import in the downloaded .aia file. This will open a project that contains the images you will need in this lesson. Then use the Projects > Save Project As option to rename your project to CoinFlip.
You can either watch the video tutorial, read the text tutorial or use the short handout.
Optional Enhancements for Part I
Before you begin these optional enhancements, use the Projects > Save Project As option to save your app from Part I as "Coin Flip Enhancements" so that you don't lose the original code which is needed for Part II.
Here are some optional creative enhancements to enhance the CoinFlip app and help build your programming skills. (text version)
 Modify the app so that the user can also shake the phone to flip the coin. (HINT: Use the Accelerometer Sensor.) NOTE: Instead of copying and pasting the coinflip algorithm, you'll want to use a procedure to reduce complexity in your code.
 Modify your app so that “heads” or “tails” is spoken when the coin is flipped. (HINT: Use the TextToSpeech component.)
 Modify the event handler in the Coin Flip app to use a random fraction block instead of random integer. (HINT: A random fraction is a decimal number between 0 and 1, not including 1. Some examples: 0, 0.25, 0.33, 0.5, 0.66, 0,75, 0.99.)
 If/else Algorithm You now have an app that can flip a twosided coin. Modify your app that so that it can flip a threesided coin. (Hint: You will need an if/else block with three conditions. You’ll need a third image for this problem; here’s one that is openly licensed: coin on edge.)
 According to this report, if you stand a bunch of Lincoln pennies on their edge and then bang the table, they have a strong bias toward coming up heads. Let’s suppose the coin has a 70% chance of coming up heads (30% tails) in this experiment. Create a model to simulate this biased coin. Hint: You will need to use the random fraction block and use a < in your if block.
 Real World Statistical Simulations: use random numbers to predict or simulate real world situations. For example, Mobile CSP teacher Ingrid Roche has her students in Boston Public Schools look at racial profiling in Boston policecivilian encounters to see if the racial demographics of the U.S. or state population match the racial demographics of police stop rates.
Tutorial Part II: Repeating the Coin Flip N Times
Open your original app from Part I (without the enhancements) and use the Projects > Save Project As option to save your app from part I as CoinFlipExperiment before continuing with this part of the tutorial. In this part, we will revise the User Interface and use a loop to repeat the coin flip N times, keeping count of the number of times it comes up heads. This will allow us to calculate the percentage of heads, which should be close to 50%. Let's see how it goes!
4.5.4. Still Curious?¶
Are coin flips fair? While it might be the case that the coin itself is fair — i.e., it favors neither heads nor tails — perhaps the act of flipping a coin is not fair. This NPR story reports on experiments that suggest that coin flips are slightly biased towards heads.
4.5.5. SelfCheck¶
Here is a table of the technical terms introduced in this lesson. Hover over the terms to review the definitions.
model
random random event random number generator 
 Choosing a ball from a black jar filled with red and green balls.
 Choosing a ball from a jar is a random event.
 Rolling a 12sided die.
 Rolling a single die is a random event.
 Counting by 2s up to 20.
 We’re in the learning zone today. Mistakes are our friends!
 Flipping a coin that has 3 sides.
 It may be difficult to imagine a 3sided coin, but we will see one in the next lesson.
 Dealing a hand of poker.
 Assuming the deck is properly shuffled, dealing a poker hand is random.
Q3: Which of the following would be examples of random events?
 1 or 3
 This is challenging, but rewarding! 1 and 3 are two of the possible values, but not all of them.
 1, 2, or 3
 This random integer block will return a random value between 1 and 3, inclusive.
 1 or 2
 This is challenging, but rewarding! 1 and 2 are two of the possible values but not all of them.
 1
 This is challenging, but rewarding! 1 is one of the possible values but not all of them.
Q4: For the following block what possible values would be assigned to X?
Q5: For the following block what value would be assigned to Label1? Type your answer into the text box. (Spelling counts. Don’t use quotes.).
blank :Tails: Don’t worry, it’s hard! Let’s go back and try it again... A. 0
 B. 0.99
 C. 0.5
 D. 1
 That's right! The randomfraction block cannot generate the value 1.
Q6: Which of the following values could NOT be generated by the randomfraction block?
Q7: For the following block what value would be assigned to Label1? Type your answer into the textbox. (Spelling counts. Don’t use quotes.).
blank :Heads: If it were easy, you wouldn’t be learning anything!Q8:
AP 2021 Sample Question: A list of numbers has n elements, indexed from 1 to n. The following algorithm is intended to display the number of elements in the list that have a value greater than 100. The algorithm uses the variables count and position. Steps 3 and 4 are missing.
Step 1: Set count to 0 and position to 1. Step 2: If the value of the element at index position is greaterthan 100, increase the value of count by 1. Step 3: (missing step) Step 4: (missing step) Step 5: Display the value of count.
Which of the following could be used to replace steps 3 and 4 so that the algorithm works as intended?
Step 4: Repeat steps 2 and 3 until the value of count is greater than 100.
Step 4: Repeat steps 2 and 3 until the value of position is greater than n.
Step 4: Increase the value of position by 1.
Step 4: Increase the value of count by 1.
4.5.6. Reflection: For Your Portfolio¶
Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Doc where you may use File/Make a Copy to make your own editable copy.