Contents | Prev | Next

7.4.41 The randomize procedure

Description

The randomize procedure initializes the random number generator with a seed. The random number generator is a psuedo random number generator (i.e. the sequence of numbers generated appears to be random but the same sequence of numbers is generated every time the same seed is used).

Parameter

The randomize procedure's only parameter is optional, and if supplied is an expression of integral type that is the seed used to initialize the random number generator. If this parameter is not supplied then the random number generator is initialized with a seed derived from the current system date and time. Specifying this parameter is useful if you want a repeatable sequence of random numbers. Or you can just not call the randomize at all in which case the the random number generator generates the same sequence of the random numbers, every time your program is run.

Example

The following program generates two random sequences of numbers. The first sequence repeats every time the program is run since the random number generated is initialed with the same seed each time the program is run. The second sequence does not repeat since the random number generator is initialized with a system generated seed each time the program is run.

program rnd(output);
var
 i : integer;
begin
 writeln('Repeatable sequence');
 randomize(6566);
 for i := 1 to 10 do
  writeln(random(10));
 writeln('Non-repeatable sequence');
 randomize;
 for i := 1 to 10 do
  writeln(random(10));
end.

Portability

Operating Systems: All
Standard Pascal: No

Contents | Prev | Next