CPSC 110-08: Computing on Mobile Phones
Spring 2011

Random Number Generators

Many of you are developing games for your first creative project and most games require a certain amount of randomness. This tutorial demonstrates how to use randomness in an App Inventor app.

First we need to distinguish between true randomness and pseudo randomness. A truly random event would be one that is completely unpredictable. If you have 100 numbered balls in jar and you thoroughly mix them up and then pick a ball and read out its number, that would be a truly random event. A sequence of numbers generated from this approach would yield a different sequence each time you did it. Other truly random events would be flipping a fair coin or drawing a card from a well shuffled deck -- you would not expect to be able to predict the top card.

A pseudo random event is one that looks random but is really deterministic, that is predictable. Mathematicians and computer scientists study and develop functions called pseudo random number generators (or PRNGs for short) for use in many different kinds of computer applications, including games, but also in cryptography and other computer security applications.

For example, here's an formula for generating a sequence of numbers that looks random:

Xn+1 = (aXn + b) mod m

This is will generate a random-like sequence of numbers between 0 and m-1. The sequence will appear random, but if you know the nth number, you can predict the n+1st number. And if you know the first number, X0, called the seed, you can predict every number in the sequence (given the constants a and b).

App Inventor's PRNGs

App Inventor uses a PRNG and you'll want to use it in your games. One of the challenges of developing an app that involves randomness is trying to fix bugs when something goes wrong. Because you (eventually) want the game to behave differently every time it's played, it can be very hard to find a bug. But we can fix things so that during development, the game behaves the same way every time it is restarted.

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 lower and upper bound arguments, inclusive
random set seed -- sets the seed for the PRNG


Let's create an App that has two buttons and two labels. The labels will be used to display a random integer each time a button is clicked. The buttons will be used to generate the next random integer and to reset the PRNG. Here's how the user interface looks:

In the blocks editor, we need to code the event handlers for each of the two buttons. Let's have the "next" button simply generate a random integer and display it. And let's have the "reset" button set the PRNG's seed to fixed value, the same value every time. Here's what your code should look like:

Start this app and click on the "next" button several times. You will be unable to predict the sequence of values that are displayed on the screen.

To fix that, however, click on the "reset" button and now click on the "next" button several times. Remember the sequence of numbers that is generated. Now click the "reset" button again and then click the "next" button several times. You should get the same sequence every time.

Lesson: In developing a game with randomness, it is useful in determining the game's correctness to be able to generate the same sequence of events every time.

But, of course, if we don't want our game to have a "Next" button on its interface, we need to find another way to seed the PRNG. App Inventor has a special event handler for its Screen1 component that allows you to perform certain actions when the app is first started:

By setting the seed to our magic number, we can fix the behavior of the app to the same sequence of events each time it is restarted. When we no longer want the same sequence, we can "Deactivate" the call to randome set seed by right-clicking on its block.

Lesson: App Inventor's Screen1 contains an Initialize procedure that is automatically called once whenever the app is restarted. To invoke it from the Blocks Editor you would select "Connect to Device".