Coin Flip - A
14
14
Coin Flip is an app that simulates the flipping of a two-sided coin. This app uses App Inventor’s random number generator and two images to simulate the coin flip.
if/else
, to evaluate a variable and follow an algorithm based on the value of a variablemodel
- an abstraction that provides a simplified representation of some complex object or phenomenon.random
- the lack of pattern or regularity. A random sequence of events has no order or pattern.random event
- an event that cannot be predicted with certainty. Examples would include flipping a fair coin, rolling a die, picking a card from a well shuffled deck.random number generator
- an algorithm that generates a sequence of numbers that seem to occur in random order. Also called a pseudo random number generator (PRNG)
.Before getting started on the Coin Flip app, try this simple experiment:
If you perform this experiment, heads will often come up 10 times, but not always, even though, 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.
CoinFlip
.The UI for the first version of our Coin Flip app will consist of two Components: a Button
and an Image
. The Button
is used to flip the coin to either heads or tails. The Image is used to display either heads or tails when the coin is flipped.
Button
from the Palette’s User Interface categoryWidth
to Fill ParentImage
component from the Palette’s User Interface categoryheads.jpg
provided in the templateTo center the image on the screen you can change the Screen1.AlignHorizontal
property
The Coin Flip app should simulate the flipping of a two-sided coin. When the user clicks the button, the coin should be flipped and land on either heads or tails. The picture should change to represent the side the coin lands on. A variable coin
will be used to represent either heads or tails and a conditional statement, if/else,
will be used to display the correct image.
Nearly all of the app’s code will be inside the Button1.Click
event handler. Begin by dragging the Button1.Click
event handler from the Toolbox onto the Blocks workspace.
How should we represent the coin that is being flipped? The answer is to use a global variable.
coin
will be a global variable that can have one of two values: 1
(for heads) or 2
(for tails). First initialize the variable by getting an initialize global variable
block from the Toolbox. Name the variable coin
and set coin
to heads by giving it an initial value of 1. Now each time Button1
is clicked, coin
should 'flip' and randomly 'land' on 1 (heads) or 2 (tails). To do this, you will need to use a setter block for coin
from the Toolbox.
To randomly set the value of coin
to be either 1 or 2, you will need to use App Inventor's random integer
block. App Inventor has several blocks for randomness in the Toolbox's Math
drawer. You may have already seen and used these blocks. Because our coin
will have a random whole number (1 or 2), we will use the second of these blocks.
random fraction: Randomly selects a number (such as 0.532) between 0 and 1 (not including 1):
random integer: Randomly selects a number between two specified whole numbers, inclusive:
Inclusive means it includes the first and last numbers, note that many programming languages are exclusive which means their random function does NOT include the last number.
Get the random integer
block from the Toolbox and use it to set the value of the coin
variable. Then, specify the range to be from 1 to 2 using the number blocks that are provided. This code should go inside the Button1.Click
handler.
Now, if you were to click the button, coin
would 'flip' and randomly 'land' on 1 (heads) or 2 (tails) but you would only see the heads.jpg
image on your screen. Let's use an if/else
statement to determine when heads.jpg
should be shown and when tails.jpg
should be shown.
To display the result of the simulated coin flip, we will either display the heads.jpg
image or the tails.jpg
image. For this, we will need an if/else
block that implements the pseudocode algorithm shown on the left below, with the corresponding App Inventor code shown on the right (AP Pseudocode in the middle).
Putting these elements together gives us the following algorithm for simulating the flipping of a two-sided coin, both in College Board-style pseudocode and in App Inventor blocks.
Now, you have a fully functioning Coin Flip app that simulates the flipping of a two-sided coin. Test out your app to make sure it works correctly. How accurately can you predict whether the next 'flip' will be heads or tails? If you can't predict any more accurately than flipping a real coin, then we have created a pretty good computer model or computer simulation of a coin flip.
App Inventor, and other computer languages, use a form of randomness called pseudo randomness. Pseudo randomness is a model (or simulation) of true randomness. Just like your app models or simulates a coin flip, the App Inventor blocks random-fraction
and random-integer, which we used to generate random numbers, are models or simulations of truly random numbers. In fact, there are algorithms in App Inventor, known as Pseudo Random Number Generators or PRNGs, that simulate the generation of random numbers. We will take a closer look at how PRNGs work in an upcoming lesson.
As we will learn later in the course when talk about encryption, the development of secure networks, such as the Internet, depends in crucial ways on the development of good PRNGs. So this is an important area of research in computer science and related fields of mathematics.
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 itself is not fair. This NPR story reports on experiments that suggest that coin flips are slightly biased towards heads.