Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread Random(0,0) <> 0
Tue, Dec 4 2018 1:49 AMPermanent Link

Michael Dreher

For all functions calls Random(n,n) I get n for the return value,
except for n=0 where I get numbers > 0. A bug? Using EWB 2.06B15.

Michael Dreher
Tue, Dec 4 2018 3:42 AMPermanent Link

Matthew Jones

Michael Dreher wrote:

> For all functions calls Random(n,n) I get n for the return value,
> except for n=0 where I get numbers > 0. A bug?

Reading the spec, it is as expected. The second param of 0 means between 0 and MaxInt.

--

Matthew Jones
Tue, Dec 4 2018 5:39 AMPermanent Link

Michael Dreher

"Matthew Jones" wrote:

   // Reading the spec, it is as expected. The second param of 0 means between 0 and MaxInt.

The help declares

function Random(AFrom: Integer=0; ATo=<MaxInt>): Integer

which means that the 2'nd parameter defaults to MaxInt when
it's not provided and not when the 2'nd parameter is provided
and of value 0.

Michael Dreher
Tue, Dec 4 2018 6:40 AMPermanent Link

Matthew Jones

Michael Dreher wrote:

> The help declares

I am indeed very wrong. Apologies. What is interesting though is that my code has quite a few calls to Random like:
'RND' + IntToStr(Ceil(Random * 255)) + '-' + IntToStr(Ceil(Random * 255));

Obviously it has multiple modes - and the JavaScript Random is a float returning a number between zero and 1.

But the fact that the one you are using is internal means we can't see what is happening.

I have to ask though, what are you really expecting to get? Presumably just zero? And what are you actually getting - what range is returned?

--

Matthew Jones
Tue, Dec 4 2018 7:27 AMPermanent Link

Michael Dreher

"Matthew Jones" wrote:

  //  I have to ask though, what are you really expecting to get?
  //  Presumably just zero? And what are you actually getting - what range is returned?

Yes zero of course. My application is indexing an array randomly

 if stringList.Count() > 0 then
      stringList[Random(0, stringList.Count() - 1)]  := '..';

which results in "List index 54653645345 out out of bounds" (the high
number differ each run), when stringList.Count() = 1, which means Random(0,0).

Michael Dreher
Tue, Dec 4 2018 9:18 AMPermanent Link

Raul

Globestar Systems

Team Elevate Team Elevate

On 12/4/2018 5:39 AM, Michael Dreher wrote:
> which means that the 2'nd parameter defaults to MaxInt when
> it's not provided and not when the 2'nd parameter is provided
> and of value 0.

If step with debugger into random then yes second param is checked for 0
and only then defaulted to maxint (or 9007199254740991).

Tim would need to take a look at this one

Raul

Tue, Dec 4 2018 2:04 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< For all functions calls Random(n,n) I get n for the return value, except for n=0 where I get numbers > 0. A bug? Using EWB 2.06B15. >>

I've fixed this for 2.06 B19.

Tim Young
Elevate Software
www.elevatesoft.com
Image