Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2017-05

Different results of random(int32) and random(int64) for negative limit value

10 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Schreiber

Hi,

FPC fixes_3_0, Linux X86:
"
var
 i1: int32;
begin
 for i1:= 0 to 5 do begin
  writeln(random(int32(-16)));
 end;
 writeln('*************');
 for i1:= 0 to 5 do begin
  writeln(random(int64(-16)));
 end;
end;
"

produces
"
-9
-9
-11
-13
-10
-13
*************
3
9
5
4
6
8
"
Is this intended? If not, which one is correct?
https://www.freepascal.org/docs-html/current/rtl/system/random.html states for 32 and 64 bit
"
Random returns a random number larger or equal to 0 and strictly less than L. "
which raises the question, what means "less"?

Martin

Reply by email

Jonas Maebe
to Martin Schreiber
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

Reply by email

Mark Morgan Lloyd
to Martin Schreiber: FPC fixes_3_0, Linux X86: " var i1: int32; begin for i1:= 0 to 5 do begin…
Martin Schreiber wrote
Hi, FPC fixes_3_0, Linux X86:"var i1: int32;begin for i1:= 0 to 5 do begin writeln(random(int32(-16))); end; writeln('*************'); for i1:= 0 to 5 do begin writeln(random(int64(-16))); end;end;" produces"-9-9-11-13-10-13*************395468"Is this intended? If not, which one is correct?https://www.freepascal.org/docs-html/current/rtl/system/random.htmlstates for 32 and 64 bit"Random returns a random number larger or equal to 0 and strictly less than L."which raises the question, what means "less"?

What does it do if the parameter is +ve? Note
https://mantis.freepascal.org/view.php?id=31693

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

José Mejuto
to Jonas Maebe: random(x) is undefined for negative parameters. It should have had an unsigned parameter,…
Jonas Maebe wrote 7 lines
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.

Hello,

At least in 3.0 (trunk not tested as I don't have it compiled) the first and second random numbers are always the same value, so:

Randomize;
A:=Random(n);
B:=Random(n);

A and B will be always the same value.

Can someone with trunk test it ?

Reply by email

Jonas Maebe
to José Mejuto
José Mejuto wrote
At least in 3.0 (trunk not tested as I don't have it compiled) the first and second random numbers are always the same value, so:

That is unrelated to this thread, but has been fixed in FPC 3.0.2.

Jonas

Reply by email

Bart
to Jonas Maebe: random(x) is undefined for negative parameters. It should have had an unsigned parameter,…
Jonas Maebe wrote
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.

I already reported that the documentation is a bit "off": https://bugs.freepascal.org/view.php?id=31642

Bart

Reply by email

Ozz Nixon
to Bart

Well, I waited to see if one of the designers would have answer it... you are not passing it a negative, nor is it responding with a negative. You are passing it with the high (MSR) bit on, Int32(-16) = 1000000000010000

The compiler is obviously type casting the result to Int32, showing as -9 etc.

I have noticed when working with Int64, FPC is actually working as UInt64 is some of my other projects.

My two cents.
O.

Earlier message 17 lines
-----Original Message----- From: fpc-devel On Behalf Of Bart Sent: Saturday, May 20, 2017 5:00 PM To: FPC developers' list Subject: Re: [fpc-devel] Different results of random(int32) and random(int64) for negative limit value On 5/20/17, Jonas Maebe wrote: > 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. I already reported that the documentation is a bit "off": https://bugs.freepascal.org/view.php?id=31642 Bart

Reply by email

Thaddy de Koning
to Jonas Maebe: random(x) is undefined for negative parameters. It should have had an unsigned parameter,…

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

Jonas Maebe wrote 13 lines
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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Thaddy de Koning
to Thaddy de Koning

Of course 64 and 32 bit are the sizes, not the platform! That may not be clear.

Thaddy de Koning wrote 56 lines
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 - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email