# EZ Programing Demos

### New

Turtle Graphics

EZ Math Movie

Zona Land Education

## Random numbers over a range

Let's think about making a random number between 2 and 7.

First, let's consider the section of the number line over which we want to get our random number. Here it is colored red:

Now, our random number will be at least equal to 2, and most of the time it will be equal to 2 plus some random percent of this red distance.

Notice that this distance is equal to 5, since 5 = 7 - 2. So we want a number equal to 2 plus some random percent of 5.

Perhaps our random percent will be 37.5%. Well, 37.5% of 5 is 1.875, and 2 plus 1.875 is 3.875. So, our random number would be 3.875, which is certainly between 2 and 7.

Maybe our random percent will be 75%. Now, 75% of 5 is 3.75, and 2 plus 3.75 is 5.75. Our random number is 5.75, again between 2 and 7.

Where do we get the random percent needed for this calculation? It turns out that this random percent is simply the output of our random number function. The random number function gives us a number between 0.0 and 0.99... . That's between 0.0% and 99.99...%. Our random number function can be thought of as a random percent function outputting from 0% up to, but not including, 100%. It's not prefect, since it does not go up to 100%.

So, we have these:

• The minimum value: 2
• The maximum value: 7
• The distance between the minimum and maximum values: 5 = 7 - 2
• A random percent: random number function, rnd()

We will combine the above into and equation. The variable x will be our random number between 2 and 7. We will calculate it by making it equal to 2 plus a random percent of the distance between 2 and 7. That looks like this (remember '*' means multiplication):

x = 2 + rnd() * (7 - 2)

Let's generalize. We want a random number between 2 and 7. So, the minimum, or min, is 2 and the maximum, or max, is 7. Therefore, we could write the above calculation using the variables min and max like this:

x = min + rnd() * (max - min)

The above calculation is printed by the following demo.

Slow  Medium  Fast
Click 'Run' to go.
 The above method will never calculate a random number equal to the maximum, 7 in the above example. That is because the value output by the random number function will never get to 1. It goes up to, but not including, 1. So, let's calculate with a number close to 1, say 0.9999, or 99.99%. That percent times 5 plus 2 is 6.9995, not quite 7.

The flowchart for this demo looks like this:

In an imaginary computer language this could look like the following:

```
min = -10;
max = 10;

print min + rnd() * (max - min);
```

Suggested next article:

EZ Math Movie is a site that can help you experiment with and understand many topics in mathematics. Its main feature is an interactive animated (x, y) graph that you control with an actual programing language. There are many examples and tutorials, and EZ Math Movie is crossed referenced with both EZ Programing Demos and Zona Land Education.

Zona Land Education is a site with explanations and interactive diagrams covering many topics in physics and mathematics. Zona Land Education is cross referenced with EZ Math Movie, and it contains several animations that use EZ Math Movie's programing language.