[Lazarus] Randomize

Michael Van Canneyt michael at freepascal.org
Tue Dec 30 18:06:34 CET 2008



On Tue, 30 Dec 2008, Lv wrote:

> Randomize seems to be 1 second dependent on the system clock.
> I cannot remember if this used to be the same in Borland Delphi/Pascal
> The example below shows a 1 second tick sensitivity to random number
> creation.
> 
> Am I missing something?
> 
> 
> uses unix,crt;
> VAR
>    dummystring,filename	   : String;
>    randomnumber	   : Int64;
>    hellfreezesover : Boolean;
> 
> 
> 
> BEGIN
>    hellfreezesover:=False;
>    Repeat
>    Randomize;
>    randomnumber:=Random(9999999999999999999);
>       writeln('randomnumber=',randomnumber);
>    Until (hellfreezesover or keypressed);
> END.

This is normal behaviour. Randomize initializes the random seed with
a value it retrieves from the clock. You must call Randomize exactly
once in  your program, never in a loop, something like this:

   Randomize;
   Repeat
    randomnumber:=Random(9999999999999999999);
    writeln('randomnumber=',randomnumber);
   Until (hellfreezesover or keypressed);

Michael.



More information about the Lazarus mailing list