SRL Archive - Help

Random(0) & Random(1)


Runaway

2012-08-23 15:54:53

Why do both of these output the same result? I can't seem to figure out the logic behind it!


program new;
var
Tally: array[0..1] of array[0..1] of Integer;
i: Integer;

begin
for i := 0 to 99 do
begin
Inc(Tally[0][Random(0)]);
Inc(Tally[1][Random(1)]);
end;
Writeln(Tally[0]);
Writeln(Tally[1]);
end.


Compiled successfully in 31 ms.
[100, 0]
[100, 0]
Successfully executed.

footballjds

2012-08-23 15:58:50

lol, inclusive bruh

random(1) = 0
random(0) = 0

the reason is because we start counting at 0…

program example;
var
i: integer;
begin
for i := 0 to 10 do
begin
writeln('Random(1) = ' +IntToStr(random(1)));
writeln('RandomRange(0, 2) = '+IntToStr(RandomRange(0, 2)));
end;
end.

RandomRange(0, 2); is one of two things, 0 or 1.

Footy

2012-08-23 16:01:27

Makes me wonder why random(0) compiles, if random(1) is 0 to 0, then random(0) is 0 to ?

footballjds

2012-08-23 16:02:47

From: Footy
Makes me wonder why random(0) compiles, if random(1) is 0 to 0, then random(0) is 0 to ?


an integer that's value hasn't been set is 0 as well…

Footy

2012-08-23 16:03:45

Thanks for clearing that up

Gu3st...

2012-08-23 16:08:07

Radnom(0) should (logically) return nil

Runaway

2012-08-23 16:15:30

Makes sense I guess…

So one more thing I'm confused about then:

if Random(0) = Random(1)
then why doesn't RandomRange(0, 2) = RandomRange(1, 2)?


program new;
var
Tally: array[0..3] of array[0..1] of Integer;
i: Integer;

begin
for i := 0 to 99 do
begin
Inc(Tally[0][Random(0)]);
Inc(Tally[1][Random(1)]);
Inc(Tally[2][RandomRange(0, 2)]);
Inc(Tally[3][RandomRange(1, 2)]);
end;
Writeln(Tally[0]);
Writeln(Tally[1]);
Writeln(Tally[2]);
Writeln(Tally[3]);
end.


Compiled successfully in 31 ms.
[100, 0]
[100, 0]
[50, 50]
[0, 100]
Successfully executed.

riwu

2012-08-24 03:14:48

Random(1) basically generates 1 number, and since we start counting from 0, that one number will be 0.

Random(0) generates 0 number, and generating nothing returns 0 too (nil=0)

RandomRange(0,1) will therefore be the same as RandomRange(0,0).

2 functions returning same value does not imply that they is equal (and therefore you cannot treat it as equivalent and apply it to other cases)
Like:
0! (factorial)=1
1! is also =1
but can you conclude that 0=1?

masterBB

2012-08-24 03:25:40

When you call random call Random(x); you ask simba for a random out of x numbers. Random(0); will return no random number, you simple ask to pick a number out of zero numbers. It will simply return 0 cause it is not possible.

RandomRange(x, y); is a different story. That function equals:
Result >= x and Result < y. x is a possible return value for this function and y is not.

So if Random(5); is called it will pick one number out of [0, 1, 2, 3, 4].
And if RandomRange(2, 5); is called it will pick one number out of [2, 3, 4].

e:
possible version of random range. I don't know the exact internal workings.
function RandomRange(x, y): Integer;
begin
Result := x + random(y - x);
end;

riwu

2012-08-24 03:30:24

From: masterBB
When you call random call Random(x); you ask simba for a random out of x numbers. Random(0); will return no random number, you simple ask to pick a number out of zero numbers. It will simply return 0 cause it is not possible.

RandomRange(x, y); is a different story. That function equals:
Result >= x and Result < y. x is a possible return value for this function and y is not.

So if Random(5); is called it will pick one number out of [0, 1, 2, 3, 4].
And if RandomRange(2, 5); is called it will pick one number out of [2, 3, 4, 5].

That is only true for delphi/some other languages.
In pascal randomrange will return the lower limit inclusive but NOT the upper limit.
i.e. RandomRange(1,2) will only return 1, while RandomRange(0,2) will only return 0,1. Basically if you want it to start from 0, just use 'Random(x)' as it will be identical to RandomRange(0,x)

masterBB

2012-08-24 03:32:43

From: riwu
That is only true for delphi/some other languages.
In pascal randomrange will return the lower limit inclusive but NOT the upper limit.
i.e. RandomRange(1,2) will only return 1, while RandomRange(0,2) will only return 0,1. Basically if you want it to start from 0, just use 'Random(x)' as it will be identical to RandomRange(0,x)


Already had it fixed in your quote. I was confused with another language ^^

e:
Oh wait not completely, left the last example.

Runaway

2012-08-24 11:46:36

Okie, thanks for clearing that up guys :)

tt_83

2015-02-18 09:44:32

Why then in mouse.simba (github.com/SRL/SRL-6/blob/master/lib/core/mouse.simba) used code like this:
 D := randomRange(3, 4);
D := randomRange(2,3);
Can you say where these functions are documented?