[fpc-devel] Different results of random(int32) and random(int64) for negative limit value

Thaddy de Koning thaddy at thaddy.com
Wed May 24 09:35:45 CEST 2017


Jonas, sorry for the late response:

The implementation is _*not *_undefined for negative values,_unless you 
say that you define it as undefined_.

Because you seem to have implemented it or most of it.

It renders a mathematical comparable distribution in the negative to the 
positive values.

In both Turbo Pascal as in Delphi and because they use a different 
algorithm and made an implementation error as well, the negative values 
are indeed not defined. But that's because of the algorithm and because 
of the implementation by Borland (yes, it stems from Borland times).

The Mersenne Twister we use is also valid for negative values and if you 
want I can send you the mathematical proof.

I already made the LCG in Delphi compatible mode available on the wiki 
and that implementation differs in so far as that it corrects the 
"undefined for negative values" for that algorithm too. It is 100% 
compatible for the Delphi documented range, btw.

I am busy evaluating important Random implementions for different 
languages, so an FPC library is available for data that is generated in 
a different language and relies on a particular PRNG.

Also note that the output of the current random is strictly valid for 32 
bit only.

In my code I already added a 64 bit version.

Regards,

Thaddy




On 5/20/2017 2:57 PM, Jonas Maebe wrote:
> On 20/05/17 14:36, Martin Schreiber wrote:
>> Is this intended? If not, which one is correct?
>
> random(x) is undefined for negative parameters. It should have had an 
> unsigned parameter, like in Turbo Pascal (where it is word). Delphi 
> defines it as always returning a positive value, but I don't know what 
> happens if you pass a negative parameter there.
>
>
> Jonas
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20170524/d76a4f7e/attachment.html>


More information about the fpc-devel mailing list