# 4.7. Pseudo Random Numbers¶

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

( Teacher Tube version)

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

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

### 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.3. Summary¶

In this lesson, you learned how to:

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

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.