[fpc-pascal]Random generator

Jonas Maebe jonas at zeus.ugent.be
Sat Dec 6 19:31:39 CET 2003

On 6 dec 2003, at 19:16, jordi wrote:

> Sorry, there are a few things that I don't understand... and I am not 
> so
> good in mathematics... and I can't explain it in English quite well.
> The same seed generates the same random sequence... or not?


> and why I can randomize as often as I want in the same program?

Randomize is simply a procedure, the compiler does not know it is 
special or so.

> A loop like this generates the same numbers:
> for loop := 1 to 100 do begin
>   randomize;
>   writeln (random (1000) +1);
> end;

You are using random/randomize wrong here. The correct way is

for loop := 1 to 100 do begin
   writeln (random (1000) +1);

Randomize initialises the random number generator based on the current 
system time. So if you call it multiple times in a short time period, 
you will get a similar (or even the same) randseed.

> How can I re-seed in the same program?

org_seed := randseed;
for loop := 1 to 100 do begin
   writeln (random (1000) +1);
randseed := org_seed;
for loop := 1 to 100 do begin
   writeln (random (1000) +1);

Now you will get twice the same 100 numbers. Do not call randomize more 
than once during a program run, it will only make the numbers you get 
"less random".


More information about the fpc-pascal mailing list