HiSoft Pascal Primer cover: a square, a triangle and a circle drawn and filled by a HiSoft Pascal program on the Tatung Einstein
← Back to Modules
module
16

Chance

Introduction

Turbo's Random(6) gave a number from 0 to 5, and a program started the same way every time until it called Randomize. HiSoft's RANDOM is different on both counts - and it has a weakness the Turbo course did not have to warn you about.

RANDOM

RANDOM is a function with no brackets and nothing to give it. It returns a whole number from 0 to 255. For a dice throw, take the remainder after dividing by 6 and add 1:

A:=RANDOM MOD 6+1;

RANDOM(6) is refused, and so is RANDOMIZE - there is none, and none needed. RANDOM does not start from a fixed point: its numbers depend on the exact moment it is called, so a program run twice starts differently.

Where Chance Really Comes From

That same dependence on the moment is its weakness. A loop that does the same work between one call and the next calls RANDOM at the same rhythm each time round, and the numbers fall into step: a loop printing twenty of them ended 34 43 52 61 70 79, nine apart. Two runs of a loop throwing ten pairs of dice differed only in the first two throws; the last eight were identical. The manual warns of it too.

The one thing on the Einstein that is never in step is you. Take the number at a moment the player chose - just after a key is pressed - and it is as good as chance gets here. The code below throws the dice only when a key is pressed.

The Code

10 (*$C-*)
20 PROGRAM DICE;
30 VAR A,B:INTEGER; C:CHAR;
40 BEGIN
50 WRITELN('ANY KEY THROWS, Q STOPS');
60 REPEAT
70 REPEAT C:=INCH UNTIL C<>CHR(0);
80 A:=RANDOM MOD 6+1;
90 B:=RANDOM MOD 6+1;
100 WRITE(A,B);
110 IF A=B THEN WRITE('DOUBLE');
120 WRITELN
130 UNTIL C='Q'
140 END.

Line 10 is there for INCH (S15).

Starting from

DICE.ASC saved from XBAS; HPEIN DICE.

What you should see

A pair of numbers from 1 to 6 for each key you press, with DOUBLE after a pair that match - for example:

ANY KEY THROWS, Q STOPS
2 4
4 4 DOUBLE
2 1

Q throws once more, then stops. Run it again and the throws are different.

Change One Thing

  • Change line 80 to A:=RANDOM(6)+1;, the Turbo way. Will it compile?
  • Put RANDOMIZE; in front of line 50. And that?
  • Change line 70 to C:=INCH;, so the loop no longer waits. What happens to the throws - and how do you stop it?

Exercises

16.1 Throw one die 600 times, count how often each number comes up, and print the six counts.

16.2 A guessing game: the program picks a number from 1 to 100 (after waiting for a key, so that it is a fair pick) and tells you "HIGHER" or "LOWER" until you guess it.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
*ERROR* at RANDOM(6)RANDOM takes nothing. Use RANDOM MOD 6.
*ERROR* 3 at RANDOMIZEThere is none; none is needed.
The same numbers keep coming up in the same orderRANDOM is being called at a steady rhythm. Take the numbers after a key press.
A key seems to be ignored in a busy loopINCH sees only a key held down at the moment it looks. Hold the key a moment longer.

Summary

RANDOM gives 0 to 255 and needs no seeding; RANDOM MOD n + 1 gives 1 to n. Called in a steady loop its numbers fall into patterns - so take them at moments the player chooses, after a key press.

Next

S17, Pictures - something Turbo could not do at all.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.