Name

Randomize Procedure

Syntax

procedure Randomize;

Description

Call Randomize once at the start of a program to start the sequence of pseudorandom numbers at a different number each time you run the program. Randomize sets the RandSeed variable to a value based on the time of day. Randomize is a real procedure.

Tips and Tricks

  • When testing a program, do not call Randomize. That way, you will get the same sequence of pseudorandom numbers every time you run the program, and you can compare results between runs. After testing, add the call to Randomize so that each time the program runs, it starts the sequence of pseudorandom numbers differently.

  • Do not call Randomize more than once in the same program. Doing so destroys the randomness of the pseudorandom numbers.

  • Randomize uses the time of day to initialize RandSeed. If the program runs at the same time every day, you should find a different way to initialize RandSeed.

Example

unit RandomDice;
interface
...
function Die: TDieRoll;
function Dice: TDiceRoll;
implementation
...
initialization
  Randomize;
end.

See Also

Random Function, RandSeed Variable

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.