[fpc-pascal] Re: random numbers

Antal antal at componente.net
Thu Mar 9 18:38:34 CET 2006


> 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.
>
> 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
I also noticed this kind of behavior before, so that program was 
speccially made to track down the problem.
I know fully understand the behavior of the Randomize, so I might suggest 
you to initialise the randseed not based on seconds, but, preferrably on 
milliseconds, which renders a better approach of randomizing.
I suppose it's not too complicated, but it can make hings better.
As I noticed, when initialized the randseed with the same value, the whole 
rand values in the chain will be totally identical, which I'm afraid is 
rather far from what we might expect from a "real" random number 
generator!

Thanks,
Antal



More information about the fpc-pascal mailing list