# 4.7. Pseudo Random Numbers¶

### Time Estimate: 45 minutes

## 4.7.1. Introduction and Goals¶

As we saw in the *Coin Flip App*, App Inventor contains blocks
that will generate random numbers and we can use those random numbers
to simulate real-world events.
But how does App Inventor create those numbers? How does a computer do
randomness?

This
Khan Academy video by Brit Cruise provides a nice conceptual overview of
*pseudorandomness* and how it differs from true randomness. The type
of pseudorandom number generator (PRNG) described in the video is different
from the type described in the lessons below. But the general principles are very much the
same.

**Learning Objectives:**I will learn to

- use modular arithmetic to produce a remainder, which can be used to create pseudorandom numbers
- recognize the difference between random and pseduorandom numbers, and the implciations of this difference on real world applications

**Language Objectives:**I will be able to

- examine a series of numbers and discuss whether or not they look random
- use target vocabulary, such as random number generator, modular arithmetic, and mod operator while considering how a computer models randomness, with the support of concept definitions and vocabulary notes from this lesson

## 4.7.2. Learning Activities¶

### Computer Randomness

It is difficult for a computer to create a truly random event.
Therefore, computers use a form of randomness known as * pseudo
randomness* -- that is, they simulate randomness.

A pseudo random event looks random but is completely predictable -- we say it is deterministic because its output can be known by someone who knows how the event was programmed. What looks random to the user is actually the result of a completely predictable mathematical algorithm.

### How does a PRNG Work

Q-2:

Suppose our PRNG generates the following sequence of numbers and suppose you seeded it with the value 11:

... 14 11 5 24 2 0 17 15 8 4 ...

What would be the next number after 11 generated by the PRNG?

Q-3:

Suppose your PRNG uses the following formula:

*X _{i+1}* = X

_{i}* 2 + 1

And suppose that *X _{1}* is 12. What value will

*X*have?

_{2}Q-4:

Suppose your PRNG uses the following formula:

*X _{i+1}* = X

_{i}* 2 + 1

And suppose that *X _{1}* is 10. What are the

**next three numbers**that the formula would generate? Type your answers into the text box, separating the numbers by a single comma.

### Clock Arithmetic and the MOD operator

The MOD operator gives the remainder when one number is divided by another. For example, 3 MOD 2 is 1 because 3 can be divided by 2 once with a remainder of 1. In the AP CSP exam, the a MOD b operator is defined as the remainder of a divided by b for positive numbers a and b. App Inventor also has a "Modulo of" block. In arithmetic expressions, the MOD operator has the same precedence as the * and / operators which means that MOD, *, and / are evaluated before + and - unless there are parentheses.

We use **modulo** 12 arithmetic every day when we read clocks with 12 hours.

Q-6: Evaluate the following expression: (8 + 14) mod 13.

Q-7: Evaluate the following expression: (8 + 34) mod 13.

Q-8:

Evaluate the following expression.

3^{3} mod 5

Q-9:

Suppose your PRNG uses the following formula:

X_{i+1} = (X_{i} * 2 + 1) *mod* 13

What would the next number be if the current number is 10?

Q-10:

Suppose your PRNG uses the following formula:

X_{i+1} = (X_{i} * 2 + 1) *mod* 13

What would the next five numbers be if the current number is 10? Separate the numbers in your sequence by commas.

If you want to practice your modular arithmetic before moving on, here are some nice exercises, with links to the answers.

### An Improved PRNG

### How Does a Slot Machine Work

Slot machines are*special purpose computers*that contain a

*random number generator*chip. This no-nonsense video explains how they work and dispels some of the many myths that surround them. The bottom line: what is the only way to win on a slot machine?.

## 4.7.4. Still Curious?¶

Learn about how a Russian crew was able to figure out how *not* to lose at slot machines in this Planet Money podcast.

Read more about linear congruential generators on Wikipedia.

PRNGs are also useful when securing the Internet, which is covered later in the course. For now, you can watch this video about CloudFlare and how lava lamps are helping to keep the Internet secure.

## 4.7.5. Self-Check¶

Here is a table of the technical terms introduced in this lesson. Hover over the terms to review the definitions.

deterministic
PRNG modular arithmetic mod operator |

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