Sign in Join

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

Mailing lists / fpc-pascal / 2017-04

Re: Bug in documentation for Random?

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Santiago A.
to Bart: http://www.freepascal.org/docs-html/current/rtl/system/random.html "Random returns a…
Bart wrote 20 lines
http://www.freepascal.org/docs-html/current/rtl/system/random.html "Random returns a random number larger or equal to 0 and strictly less than L" However random(a negative number) returns a number <= 0 and > L program r; begin randomize; writeln('Random(-10) = ',Random(-10)); end. C:\Users\Bart\LazarusProjecten>test random(-10) = -1 C:\Users\Bart\LazarusProjecten>test random(-10) = -4 Maybe my understanding of English ("strictly less than") is not correct, so I ask here before posting as a bugreport.

I've never used negative values, so this is just a guess: random(+10)
will output numbres 0..9

random(-10)
will output numbres -9..0

so, it is equivalent to
-random(10)

Saludos Santi
Bart

http://www.freepascal.org/docs-html/current/rtl/system/random.html

"Random returns a random number larger or equal to 0 and strictly less than L"

However random(a negative number) returns a number <= 0 and > L

program r;
begin
  randomize;
  writeln('Random(-10) = ',Random(-10));
end.

C:\Users\Bart\LazarusProjecten>test
random(-10) = -1
C:\Users\Bart\LazarusProjecten>test
random(-10) = -4

Maybe my understanding of English ("strictly less than") is not correct, so I ask here before posting as a bugreport.

Bart

Giuliano Colla
to Bart

Il 05/04/2017 18:09, Bart ha scritto:

Show quoted text 21 lines
http://www.freepascal.org/docs-html/current/rtl/system/random.html "Random returns a random number larger or equal to 0 and strictly less than L" However random(a negative number) returns a number <= 0 and > L program r; begin randomize; writeln('Random(-10) = ',Random(-10)); end. C:\Users\Bart\LazarusProjecten>test random(-10) = -1 C:\Users\Bart\LazarusProjecten>test random(-10) = -4 Maybe my understanding of English ("strictly less than") is not correct, so I ask here before posting as a bugreport.

My scant knowledge of English leads me to believe that the sentence should be rephrased in order to cover the negative numbers case:

either:

Random return a random number with the same sign of L, and whose absolute value is larger or equal to zero and strictly less than the absolute value of L.

or:

Random return a random number in the range 0 (included) to L (excluded).

Just my 2c.

Giuliano

Bart
to Santiago A.: I've never used negative values, so this is just a guess: random(+10) will output numbres…
Santiago A. wrote
random(-10) will output numbres -9..0

That was not realy the question.
The question is wether the documentation is wrong, or the implementation or neither.

B.t.w. Delphi7 gives me:
Random(-10) = -776108169
Random(-10) = 244426112
Random(-10) = -1774796928
Random(-10) = 1443026331
Random(-10) = -919853036
Random(-10) = -747947900
Random(-10) = -1767321924
Random(-10) = -2114448955
Random(-10) = 1499268340
Random(-10) = -1658783534
(which makes no sense at all to me...)

Bart

Lukasz Sokol
to Bart: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Random "random returns a random number…
Bart wrote 6 lines
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Random "random returns a random number between 0 and abs(num-1)" IIUC, then Random() shoud always return a positive number.

Mmm, I don't think so:

For y := Random(num);
the unsigned value of 'y' will will between 0 and abs(num-1) - inclusive,
but then, the value returned keeps the sign of num;

(this as per FPC implementation)

I like Giuliano's definition, it is brief and beautiful:

> Random(L) returns a random number in the range 0 (included) to L (excluded).

(maybe with a similarly brief and concise remark: 'regardless of the sign of L'

Bart

-L.