# 4.5. Coin Flip Simulation Tutorial¶

## 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 create an artifact that uses randomness and simulates a model create a simple model of a coin flip use the random number block to generate random values in a specific range use a conditional statement, IF/Else, to evaluate a variable and follow an algorithm based on the value of a variable use a For each number loop to repeatedly simulate the flipping of the coin Language Objectives: I will be able to use target vocabulary, such as model, random, random event, and random number generator while describing app features and User Interface with the support of concept definitions and vocabulary notes from this lesson

## 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 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)

1. 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.
2. Modify your app so that “heads” or “tails” is spoken when the coin is flipped. (HINT: Use the TextToSpeech component.)
3. 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.)
4. 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.)
5. 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.
6. 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!

## 4.5.3. Summary¶

In this lesson, you learned how to:

## 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. Self-Check¶

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

## 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.

You have attempted of activities on this page