[fpc-pascal] Re: Random numbers

Jonas Maebe jonas.maebe at elis.ugent.be
Mon Mar 6 13:39:18 CET 2006


On 6 mrt 2006, at 13:37, Antal wrote:

>> The Mersenne Twister Free Pascal uses is one of the best PRNGs known
>> today, it just has to be used the right way. But calling it from
>> several threads and "randomly" overwriting its state array is
>> definitely not the right way to use it.
> I'm disappointed, because I compiled the following program (on linux):
> rnd.pp
> begin
> Randomize;
> writeln(random(394));
> end.
>
> and I ran it for 11 times, and the results are rather strange:
> 310.
> 198
> 344
> 322*
> 322*
> 317-
> 317-
> 192
> 336.
> 382.
> 339.
> (upon noting the dups I had to re-run again twice, getting:)
> 120
> 297.

There is nothing strange about this. Randomize initialises randseed  
based on the system clock value expressed in seconds. So if you run  
it twice in the same second, you will get the same number.

> so, I've got two times a repetition, though I only re-ran the program.
> (noted with * and - )
> Then I made a new series of random generating:
>
> 297.
> 120
> 310.
> 382.
> 336.
> 192
> 317
> 322
> 344
> 198
> 339.
>
> I only have two problems with these random numbers:
> Firstly, I can notice a repetition of some "random" numbers (noted  
> with ".")
> Then also 70% of them are 3xx
> I learnt about random numbers, that they do not behave by this way.

You are not generating a series of random numbers based on the  
Mersenne Twister here, because you only generate one random number  
per program run. If you take all these values "together" as  
supposedly belonging to a "series of random numbers", then the result  
is that you are using a different "random" generator, where the next  
"random" number is generated based on the next clock/time value as  
opposed to the previously generated random number.

To test it properly, you should change your test program into this:

var
   i: longint;
begin
Randomize;
  for i := 1 to 20 do
   writeln(random(394));
end.

And then you have to compare the random numbers generated in one such  
a program run, not between different runs.


Jonas



More information about the fpc-pascal mailing list