Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
Coin Flip Simulation Tutorial¶
Time Estimate: 45 minutes
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
A Short ExperimentBefore 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 BlockIn App Inventor, we will use the random block to get pseudo-random 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 coin-flip 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 two-sided coin. Modify your app that so that it can flip a three-sided 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 police-civilian 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!
In this lesson, you learned how to:
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.
Here is a table of the technical terms introduced in this lesson. Hover over the terms to review the definitions.
random number generator
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.
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.