CPSC 110-08: Computing on Mobile Phones
Spring 2012
How to Flip A Coin, Part II

CS Principles

This activity addresses the concepts of abstraction. It provides a simple illustration of how simulation and modeling are used in computing. It focuses, in part, on the following learning objectives:

Preliminaries

This lesson assumes you have completed the tutorial for Part I.

Randomness in App Inventor

You can find the random functions in the Blocks Editor under the Math menu, where you find:

  • random fraction -- returns a random value between 0 and 1

  • random integer -- returns a random integer between its from and to arguments, inclusive

  • random set seed -- sets the seed for the PRNG

A PRNG Demo

Let's start by playing with App Inventor's PRNG. We want to see how we can control the pseudo random numbers it generates. The app consists of two buttons and label. One button lets us reset the sequence of pseudo random numbers by setting the value of the first number or seed. The other let's us generate the next random value. The label simply displays the random value (click to enlarge):

The app consists of three blocks.

Download and Experiment

Download the source code and upload it into your instance of App Inventor (MyProjects > More Actions > Upload Source). Try:

Important Points

The CoinFlipper App -- Part 2 -- Tossing the Coin

This assumes you have completed Part 1 of the app. One limitation of Part 1 is that it just toggles the coin between Heads and Tails. It doesn't really simulating tossing the coin, where the coin rotates in the air until it hits the ground.

To remedy this, let's add a Clock component and use it to simulate the flipping of the coin between heads and tails. The idea is that the coin will flip from heads to tails and vice versa every time the clock ticks.

Drag a Clock component onto your app:

Procedural Abstraction

Let's organize our algorithms into the following set of procedures. This will allow us to think about the app at a higher level of abstraction -- i.e., at a level that has operations such as FlipTheCoin, ShowTheCoin, and AnnounceResult.

Define each of these procedures, all of which use an if/else control structure:

Algorithms

In Pseudocode we would express the algorithms as follows:
# Comment: Flipping the coin is toggling between 1 and 2
To FlipTheCoin:
   If Coin = 1 then-do:
      Set Coin to 2
   else-do
      Set Coin to 1
#Comment: Display either the heads or tails image
To ShowTheCoin:
    If Coin = 1 then-do:
       Set Image.Picture to heads.png
    else-do:
       Set Image.Picture to tails.png
#Comment: Say either "Heads" or "Tails"
To AnnounceResult:
    If Coin = 1 then-do:
       TextToSpeech.Speak "Heads"
    else-do:
       TextToSpeech.Speak "Tails"

Simulating A Coin Toss

Our simulation of tossing the coin will toggle between heads and tails each time the clock ticks, displaying the coin after each clock tick. But it has to do this a random number of times to simulate a coin flipping over-and-over until it hits the ground.

Data Abstraction: Let's use another global variable named Flips to represent how many times the coin flips over.

Algorithm: Here's our algorithm. Notice how easy it is to grasp at this high level of abstraction. By using procedures we are able to hide some details:

#Comment: Simulate a coin flipping over Flips times:
When Clock.Timer clicks do:
   FlipTheCoin
   ShowTheCoin
   Set Flips to Flips - 1
   if Flips = 0 then-do:
      Set Clock.Enabled to false    # Stop the clock
      AnnounceResult

Toss the Coin

To complete this simulation all we need to do is start (i.e. enable) the Clock when the button is clicked:

Implement these changes and test your app.

Version 3 Challenge: Accelerometer Sensor

Instead of pressing a button, what if we could "toss the coin" by simply "flipping the phone". This can easily be done using App Inventor's AccelerometerSensor. Using the accelerometer will allow us to remove the Button from the interface. We can just rely on "flipping the phone" to simulate tossing the coin.

HINT: The acceleromter can detect motion ong 3 axes, the x-axis, y-axis, and z-axis. To simulate a coin flip, you will want to detect changes in the z-axis. When the phone is lying flat on its back, its z-acceleration is about 9.8 (due to gravity). When it is lying face down, its acceleration is around -9.8.