[fpc-pascal] Random numbers

Michalis Kamburelis michalis at camelot.homedns.org
Mon Mar 6 09:58:43 CET 2006


Antal wrote:
>>
>> It's not just that, but randseed is not a threadvar. This means that
>> all your threads use the same randseed variable, so if two threads
>> call "random" at the same time they will still get the same "random"
>> number.
>>
>>
>> Jonas
> 
> And also generating a rand * getpid you can't really get good random 
> numbers.
> I just made a withdrawal program on this idea, so I've got several 
> repetitions, and upon executing a program again and agian I've got false 
> random numbers, like: a*n; a*(n+1); a*(n+2).
> That's not a solution anyway.
> 
> Maybe a bit shifting or some aritmetical function can help to obtain a 
> more random "looking" number.
> 

Like Vinzent pointed out, such tricks are not a really good solution. It 
seems that most of the thread "Better random numbers ?" concentrated on 
my trick with "RandSeed := RandSeed * getpid;". But this *was meant to 
be* just a dirty hack, dirty idea, dirty workaround, whatever.

If you want a real, good solution, see the first part of my initial 
response. Quoting myself:

"
The proper solution is to use some system-wide generator, that doesn't 
have to be initialized every time you initialize your program. On Unix, 
just read /dev/urandom, this should return pseudo-random numbers. On 
Windows I guess that you will have to do something like that on your own 
(e.g. write a program in FPC that runs as a service and can be queried 
for random numbers). (Or you can use Cygwin routines to read Cygwin's 
/dev/urandom --- never done it, but it should be possible).
"

In other words, don't waste too much time trying to improve the 
"RandSeed := RandSeed * getpid;" trick.

Michalis



More information about the fpc-pascal mailing list