Results 1 to 13 of 13

Thread: Random(0) & Random(1)

  1. #1
    Join Date
    Jan 2008
    Location
    C:\
    Posts
    1,483
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default Random(0) & Random(1)

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

    Simba Code:
    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.

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

  2. #2
    Join Date
    Feb 2007
    Location
    PA, USA
    Posts
    5,240
    Mentioned
    36 Post(s)
    Quoted
    496 Post(s)

    Default

    lol, inclusive bruh

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

    the reason is because we start counting at 0...

    Simba Code:
    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.
    Last edited by footballjds; 08-23-2012 at 08:02 PM.

  3. #3
    Join Date
    Jun 2012
    Posts
    2,182
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Makes me wonder why random(0) compiles, if random(1) is 0 to 0, then random(0) is 0 to ?
    Thx Euphemism and Vinyl for the awesome siggy and avatar!

  4. #4
    Join Date
    Feb 2007
    Location
    PA, USA
    Posts
    5,240
    Mentioned
    36 Post(s)
    Quoted
    496 Post(s)

    Default

    Quote Originally Posted by Footy View Post
    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...

  5. #5
    Join Date
    Jun 2012
    Posts
    2,182
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks for clearing that up
    Thx Euphemism and Vinyl for the awesome siggy and avatar!

  6. #6
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Radnom(0) should (logically) return nil

  7. #7
    Join Date
    Jan 2008
    Location
    C:\
    Posts
    1,483
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default

    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)?

    Simba Code:
    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.

    Progress Report:
    Compiled successfully in 31 ms.
    [100, 0]
    [100, 0]
    [50, 50]
    [0, 100]
    Successfully executed.
    Last edited by Runaway; 08-23-2012 at 08:20 PM.

  8. #8
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    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?

  9. #9
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    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.
    Simba Code:
    function RandomRange(x, y): Integer;
    begin
      Result := x + random(y - x);
    end;
    Last edited by masterBB; 08-24-2012 at 07:30 AM.
    Working on: Tithe Farmer

  10. #10
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    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)

  11. #11
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    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.
    Working on: Tithe Farmer

  12. #12
    Join Date
    Jan 2008
    Location
    C:\
    Posts
    1,483
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

  13. #13
    Join Date
    Apr 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •