Sentinels and Random Numbers
When nobody knows how many inputs are coming
A for loop has to settle its trip count before it starts. Some programs cannot supply one. A practice tracker takes one entry at a time, and only the person typing knows which entry is the last. The loop has to keep going until the input itself says to stop.The value that says stop is called a sentinel. It is not data, and it is never added, counted or averaged. Its only job is to end the loop.
prompt() hands back text, always
Whatever gets typed, prompt() returns a string. Type 0 and what comes back is "0", one character of text, not the number 0.That is why the loop above is written
reply !== "0", with quotation marks. Leave them off and you get reply !== 0, which compares text against a number. Those are never equal, so the condition never turns false and the loop runs until the page is closed. One pair of quotation marks is the whole fix.Use Number() to convert, but only at the moment you are ready to do arithmetic with the value.
The program below adds up the laps a swimmer types in, one day at a time, and stops at the sentinel -1. Run it, type a few whole numbers, and then type -1. Then change it and run it again:
• Run it again and type -1 at the first prompt.
• Change
Number(entry) to entry.• Press Reset, then remove the quotation marks around -1 in the
while line. Run it, type a number, and then type -1. Press Stop to end it.let laps = 0;
let days = 0;
let entry = prompt("Laps swum today, or -1 to finish: ");
while (entry !== "-1") {
laps = laps + Number(entry);
days = days + 1;
entry = prompt("Laps swum today, or -1 to finish: ");
}
console.log("Days logged: " + days);
console.log("Total laps: " + laps);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Random numbers need nothing imported
There is no random module to load. Math is part of the language itself, so Math.random() can be called from anywhere in any program.Math.random() hands back a decimal. It can be 0. It is always less than 1. It is never 1. A run might give 0.4172, then 0.9931, then 0.0058, then something else entirely.
The recipe
To get a whole number somewhere from low through high, count how many values that is, multiply, floor, then slide:let count = high - low + 1;
let pick = Math.floor(Math.random() * count) + low;
The multiplier decides how many different values are possible. The number added on the end decides where that run of values begins. They do two separate jobs, and mixing them up is the classic mistake.
| Expression | Possible values | How many |
|---|---|---|
Math.floor(Math.random() * 3) | 0, 1, 2 | 3 |
Math.floor(Math.random() * 3) + 1 | 1, 2, 3 | 3 |
Math.floor(Math.random() * 3) + 10 | 10, 11, 12 | 3 |
Math.floor(Math.random() * 10) + 50 | 50 through 59 | 10 |
Math.random() gives a different number on every run, so the program below uses fixed stand-ins in its place. They are 0, the smallest value Math.random() can return, then 0.5, then 0.9999, close to the largest. For each one it prints the pick the recipe gives for
low through high. Run it, then change it and run it again:• In
let high = 25;, change 25 to 35.• Press Reset, then change
high - low + 1 to high - low.• Press Reset, then add
Math.random() as a fourth item in draws, and run the program several times. The last pick changes from run to run but always stays from 20 through 25.let low = 20;
let high = 25;
let count = high - low + 1;
let draws = [0, 0.5, 0.9999]; // stand-ins for Math.random()
for (const r of draws) {
let pick = Math.floor(r * count) + low;
console.log("r is", r, "and pick is", pick);
}
console.log("Possible picks:", count);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Stopping on a count, not on a list
Some loops end once enough of something has happened, and there is no way to know in advance how many passes that takes.let tries = 0;
let wins = 0;
while (wins < 4) {
tries = tries + 1;
if (Math.floor(Math.random() * 4) + 1 === 1) {
wins = wins + 1;
}
}
console.log(tries);
The counter climbs on some passes and not others, so tries comes out different every run. A for loop cannot express this. It would have to fix the number of passes before the first draw, and the whole point is that nobody can.