Log in

View Full Version : Random(0) & Random(1)



Runaway
08-23-2012, 07:54 PM
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
08-23-2012, 07:58 PM
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
08-23-2012, 08:01 PM
Makes me wonder why random(0) compiles, if random(1) is 0 to 0, then random(0) is 0 to ?

footballjds
08-23-2012, 08:02 PM
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
08-23-2012, 08:03 PM
Thanks for clearing that up

Zyt3x
08-23-2012, 08:08 PM
Radnom(0) should (logically) return nil

Runaway
08-23-2012, 08:15 PM
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
08-24-2012, 07:14 AM
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
08-24-2012, 07:25 AM
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
08-24-2012, 07:30 AM
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
08-24-2012, 07:32 AM
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
08-24-2012, 03:46 PM
Okie, thanks for clearing that up guys :)

tt_83
02-18-2015, 02:44 PM
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?