[fpc-pascal]Random generator

Jonas Maebe jonas at zeus.ugent.be
Sat Dec 6 20:40:53 CET 2003


On 6 dec 2003, at 20:26, jordi wrote:

>> 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.
>>
> With this, you have answered perfectly my question, to get a diferent
> sequence can I set randseed in a diferent way?

There are no different sequences. All pseudo-random generators have one 
circular sequence. The better the generator, the longer this sequence 
is.

> for example:
>
> Procedure set_new_random_seed;
>   begin
>     randseed := who knows yet...
>   end;
>
> For loop := 1 to 100 do begin
>   set_new_random_seed;
>   writeln (random (1000) +1);
> end;

You mean you want to write something like the "first" random number of 
100 "different sequences"?

> Thank you very much.

Note that you *cannot* save randseed in the middle of a sequence, and 
later restore it again to continue that particular sequence. The reason 
is that the random generator internally uses 3 random seeds (in 1.0.x) 
and several more in 1.1. If you change randseed, the random generator 
is reinitialised with this new seed, but the resulting sequence is 
different from what you would have gotten if you just continued 
previously (since only one of the randseeds is initialised by you, the 
rest is based on that one value).

This is different from TP/Delphi, which use only one randseed, so 
saving/restoring this one randseed is enough to save/restore the whole 
of the random generator. However, it still allows for perfectly 
reproducible random number sequences (if you set randseed to 5, print 
some random number and then set randseed again to 5, the following 
numbers will be the same as the first ones, because the newly 
calculated state will be the same as the first one).


Jonas





More information about the fpc-pascal mailing list