SRL Archive - Bugs

RandomRange


Tickyy

2014-06-27 08:39:46

program new;
var
i:integer;
begin
Repeat
i:=randomrange(0, 1)
Writeln(inttostr(i));
wait(200);
until(false);
end.

A RandomRange between 0 and 1, isn't it supposed to include 1 aswell? because this returns 0s only. It never includes the last digit of the range for example (1, 100), 100 never appears.

or is this just how randomrange is supposed to work?

Olly

2014-06-27 10:21:47

Weird, if your using Lape you can do Random(0, 1); and it will work as planned. :)

Tickyy

2014-06-27 11:06:13

From: Olly
Weird, if your using Lape you can do Random(0, 1); and it will work as planned. :)


Well random in pascalscript is something else, because it starts from 0 to n-1, but a randomrange is something different, it is supposed to include the last digit isn't it?

Turpinator

2014-06-27 13:48:13

It's due to how RandomRange is computed.
if you were going to write your own random range function (to get the same results), it would be this.
function RandomRange(a, b: integer):integer;
begin
result := a + Random(b - a);
end;

So. this would use random(b-a) so in your case, random(1)… which random(1) = 0.
so it will always be 0+0.

Could just write your own func (or just remember to add one to your higher number)
function RandomRange2(a, b: integer):integer;
begin
result := a + Random(b - a + 1);
end;

Tickyy

2014-06-27 15:50:09

I am on my phone right now but wouldnt that just do the oposite of this problem lets say randomrange (0,1) now instead of 0s it would return always 1