
A game that does the same thing every time is a puzzle - which is fine, and Warehouse is one. But most games need a little chance: a die, a shuffle, a monster that wanders. Computers are the opposite of chancy, so Turbo gives you a way of making numbers that look random - and a way of making them different each time.
Random(N) gives a whole number from 0 to N - 1, apparently at random. Random(6) gives 0, 1, 2, 3, 4 or 5 - so a die is
Die := Random(6) + 1;
A coin is Random(2): 0 or 1, heads or tails. A random capital letter is Chr(Ord('A') + Random(26)).
Random on its own, with no brackets, gives a real number from 0 up to (but not reaching) 1 - useful for chances that are not whole numbers.
Throw a die six hundred times and each face comes up close to a hundred times - not exactly, as with a real die.
Here is the catch. Start Turbo, run a program that prints twelve Random(6)s, and you get a list of numbers. Restart the Einstein, run it again, and you get exactly the same list. The numbers are worked out by a formula from a starting point, and without being told otherwise, Turbo always starts from the same place.
(Run the program a second time without restarting and the list is different - the formula carries on from where it stopped. Only a fresh start repeats.)
Randomize, once, at the start of the program, makes Turbo start the formula from a new place each time. Put it first in any program where chance should really be chance - and leave it out while you are finding a fault, when the same numbers every time are exactly what you want.
program Roll;
var
Die: Integer;
Ch: Char;
begin
Randomize;
Writeln('Any key rolls, Q stops');
repeat
Read(Kbd, Ch);
Die := Random(6) + 1;
Writeln('You rolled ', Die);
until UpCase(Ch) = 'Q';
end.
Starting from
Turbo freshly started, Y, a new work file called ROLL.
What you should see
A roll for every key you press, each a number from 1 to 6:
Any key rolls, Q stops
You rolled 1
You rolled 6
You rolled 4
Your numbers will not be these. The Q that stops it rolls once itself, because the program rolls before it checks the key.
Randomize. Restart the Einstein and run it, noting the first few rolls; restart and run it again. What do you notice?Random(6) + 1 to Random(6). Which numbers can you get now?if Random(2) = 0 then Writeln('Heads') else Writeln('Tails'); in place of the two lines that roll and print.25.1 Print ten random capital letters on one line.
25.2 Toss a coin a hundred times and print how many heads and tails.
25.3 Throw two dice a thousand times and count how often they total seven.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| The same "random" numbers every time | No Randomize at the start - and the Einstein was restarted between runs. |
| A die sometimes shows 0, never 6 | Random(6) is 0 to 5. Add 1. |
| An exercise that needs the same numbers every time keeps changing | Randomize is in. Take it out while you test. |
Random(N) gives a whole number from 0 to N - 1; Random a real from 0 up to 1. Without Randomize, a freshly started Turbo produces the same sequence every time; with it, the sequence starts somewhere new. Dice, coins and letters are Random plus a little arithmetic.
S26, Reaching The Machine - reading and writing the Einstein's memory and ports directly, and a character of your own design.