Random Numbers

Random numbers generated by the computer enable programs covering a wide range of games and activities to be developed. For example, in a program to test mental arithmetic, the problems to solve will be unpredictable since the numbers to add, subtract, multiply or divide will be chosen at random. There are three ways of using random functions in Delphi:
  1. The statement RealNum := random; will give the real variable RealNum a random number between 0 and 1. If you need a random integer, the following require less thought.
  2. The statement IntSize := random(4); will give the integer variable IntSize the value 0, 1, 2 or 3.
  3. The statement IntSize := randomRange(1, 4); will give the integer variable IntSize the value 1, 2, 3 or 4. In order to use randomRange, you need to include Math in the uses section e.g.
    uses
      SysUtils, Math;
In each case, in order to produce different random number sequences each time the program is run, it is necessary to call randomize before calling the random function for the first time. The first example uses randomRange and the second uses random.

Example 1: Using function randomRange

program Subtraction;
{$APPTYPE CONSOLE}
uses
  SysUtils, Math;
var
  Int1, Int2, Difference, UserAnswer : integer;
begin
  randomize;
  repeat
    Int1 := randomRange(1,100);
    Int2 := randomRange(1,Int1);
    Difference := Int1 - Int2;
    write('What is ',Int1,' - ',Int2,'? ');
    readln(UserAnswer);
    if UserAnswer = Difference then
      begin
        writeln('Correct!');
      end
    else
      begin
        writeln('Wrong! The answer is ',  Difference);
      end;
  until false;
end.

Example 2: Using function random

program RandomGoals;
{$APPTYPE CONSOLE}
uses
  SysUtils;
const
  GAME_LENGTH = 5;
  SLEEP_TIME = 2000;  //2 seconds 
var
  HomeGoals, AwayGoals, Count : integer;

procedure HomeGoal;
begin
  writeln('Home goal');
  HomeGoals := HomeGoals + 1;
  writeln;
end;

procedure AwayGoal;
begin
  writeln('                              Away goal');
  AwayGoals := AwayGoals + 1;
  writeln;
end;

procedure HomePenalty;
begin
  writeln('Home penalty');
  writeln;
  sleep(SLEEP_TIME);
  if random(3) > 0 then
    begin
     writeln('Home goal scored from penalty');
     writeln;
     HomeGoals := HomeGoals + 1;
    end
  else
    begin
     writeln('Penalty missed!');
     writeln;
    end;
end;

procedure AwayPenalty;
begin
  writeln('                              Away penalty');
  writeln;
  sleep(SLEEP_TIME);
  if random(3) > 0 then
    begin
      writeln('                              Away goal scored from penalty');
      AwayGoals := AwayGoals + 1;
      writeln;
    end
  else
    begin
      writeln('                              Penalty missed!');
    end;
end;
begin
  HomeGoals := 0;
  AwayGoals := 0;
  randomize;
  for Count := 1 to GAME_LENGTH do
    begin
      sleep(SLEEP_TIME);
      case Random(4) of
         0 : HomeGoal;
         1 : AwayGoal;
         2 : HomePenalty;
         3 : AwayPenalty;
      end; //of case
    end;//of for loop
  sleep(SLEEP_TIME);
  writeln;
  writeln('Game over');
  writeln;
  writeln('Score: ', HomeGoals, '-', AwayGoals);
  readln;
end.
Features introduced:
  • Use of randomize before the first random function
  • Use of randomRange and random functions
  • Use of sleep function to introduce a delay

Experimenting

Change the SLEEP_TIME in the RandomGoals program from a constant to a variable that is given a value chosen at random.

Develop a game of chance such as rock-paper-scissors in which the user can play against the computer.

Develop a mental arithmetic tester giving the user a choice of addition, subtraction, multiplication or division.

Programming - a skill for life!

Pascal Programming Tutorials