Results 1 to 12 of 12

Thread: rotateCamera(); and randomChance();

  1. #1
    Join Date
    Dec 2011
    Posts
    2,147
    Mentioned
    221 Post(s)
    Quoted
    1068 Post(s)

    Default rotateCamera(); and randomChance();

    randomChance();
    This is an easier way of having a certain chance of doing a task, saving a few lines every time you want to do something like this. randomChance(2) has a 50% chance of returning true, randomChance(3) has a 33% chance of returning, randomChance(4) is 25%, et cetera.

    Edit: We both misunderstood how random(2) worked, so basically this function combines if "random(2) = 0 or 1 then" to "if randomChance(2) then"
    Simba Code:
    function randomChance(randomNumber: integer): boolean;
    begin
      if randomNumber and (random(1, randomNumber) = 1) then
        result := true;
    end;

    Example Usage:
    Simba Code:
    if randomChance(10) then writeln('You won the lottery!');

    rotateCamera();
    This is a function that randomly moves the camera on both axes. It is more useful in OGL scripts when trying to search for objects that may have evaded view. Utilizes randomChance(); as described above. Also useful in color scripts when having north or aerial orientation isn't required.
    Simba Code:
    procedure rotateCamera;
    const
      KEYS_HORIZONTAL: TIntegerArray = [0, 37, 39];
      KEYS_VERTICAL: TIntegerArray = [0, 38, 40];
    var
      pressKeyHorizontal, pressKeyVertical: integer;
    begin
      pressKeyHorizontal := KEYS_HORIZONTAL[random(0, 2)];
      pressKeyVertical := KEYS_VERTICAL[random(0, 2)];
      if randomChance(2) then
      begin
        if (pressKeyHorizontal > 0) then
          keyDown(pressKeyHorizontal);
        if (pressKeyVertical > 0) then
        begin
          if (pressKeyHorizontal > 0) then
            wait(random(350));
          keyDown(pressKeyVertical);
        end;
      end
      else
      begin
        if (pressKeyVertical > 0) then
          keyDown(pressKeyVertical);
        if (pressKeyHorizontal > 0) then
        begin
          if (pressKeyVertical > 0) then
            wait(random(350));
          keyDown(pressKeyHorizontal);
        end;
      end;
      if (pressKeyHorizontal > 0) or (pressKeyVertical > 0) then
        wait(random(750));
      if randomChance(2) then
      begin
        if (pressKeyHorizontal > 0) then
          keyUp(pressKeyHorizontal);
        if (pressKeyVertical > 0) then
        begin
          if (pressKeyHorizontal > 0) then
            wait(random(350));
          keyUp(pressKeyVertical);
        end;
      end
      else
      begin
        if (pressKeyVertical > 0) then
          keyUp(pressKeyVertical);
        if (pressKeyHorizontal > 0) then
        begin
          if (pressKeyVertical > 0) then
            wait(random(350));
          keyUp(pressKeyHorizontal);
        end;
      end;
    end;

    Example Usage (on repeated loop):

    A single usage is one of those camera nudges.

    Credits to my buddy @Obscurity.
    Last edited by Clarity; 10-14-2014 at 04:10 AM.

  2. #2
    Join Date
    Aug 2014
    Location
    Australia
    Posts
    932
    Mentioned
    53 Post(s)
    Quoted
    495 Post(s)

    Default

    Interesting, thanks for sharing.

    I don't understand how randomChance works, could you explain it for me?



    New to scripting? Procedures & Functions for Beginners
    Do you use your computer at night? Just get f.lux

  3. #3
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    RandomChance is the same as RBool from SRL-5 !

  4. #4
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

    Default

    Couldn't you just go:

    Simba Code:
    function randomChance(randomNumber: integer): boolean;
    begin
      result := random(randomNumber) = 0;
    end

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Incurable View Post
    Interesting, thanks for sharing.

    I don't understand how randomChance works, could you explain it for me?
    It simply generates a random number in the range of [1..randomNumber] where randomNumber is the given number then it checks the generated number is 1.
    Well first if checks if the given number (read: RandomNumber) is 0, if that's the case then it just skips and returns False.

    Basically:
    So: Result := Random(somenumber) = 0;

    It can all be pressed in to one line:
    Simba Code:
    function RandomChance(randNum: integer): boolean;
    begin
      Result := randNum and (Random(1, randNum) = 1);
    end;
    (could be shortened even more if you start at 0 and not at 1)
    Last edited by slacky; 10-14-2014 at 01:44 AM.
    !No priv. messages please

  6. #6
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    I wanted it to be a random chance from 1 to x, The Mayor. Using yours, randomChance(2) would be a 33% chance, not a 50%. :P.

    I like yours, Slacky. Thanks.

    EDIT: Clarity, the version I sent you didn't have chance. Chance is not the chance that nothing will happen - it's the chance that it'll press a vertical key before a horizontal key - if it decides to press any at all. The 0 in the arrays are the 1 in 3 chance that it won't press anything. So, technically, it has a ~10% chance to press nothing at all.
    Last edited by Obscurity; 10-14-2014 at 01:46 AM.

  7. #7
    Join Date
    Dec 2011
    Posts
    2,147
    Mentioned
    221 Post(s)
    Quoted
    1068 Post(s)

    Default

    Quote Originally Posted by Incurable View Post
    Interesting, thanks for sharing.

    I don't understand how randomChance works, could you explain it for me?
    randomChance(2) means a 1 in 2 (50%) chance. If you have a procedure:
    Simba Code:
    myProcedure;

    And you want a 50% chance of the procedure happening in your script, you would write:
    Simba Code:
    if randomChance(2) then myProcedure;

    And so on:
    Simba Code:
    if randomChance(2) then myProcedure; //1 in 2 (50%) chance
    if randomChance(3) then myProcedure; //1 in 3 (33.33%) chance
    if randomChance(4) then myProcedure; //1 in 4 (25%) chance
    if randomChance(5) then myProcedure; //1 in 5 (20%) chance
    if randomChance(6) then myProcedure; //1 in 6 (16.67%) chance
    if randomChance(7) then myProcedure; //1 in 7 (14.3%) chance

    Quote Originally Posted by Olly View Post
    RandomChance is the same as RBool from SRL-5 !
    Honestly didn't know that was there, I've never looked at SRL-5, no copying intended! If that's the case, can I add it to SRL-6?

    E: Ninja'd by Obscurity.

    Quote Originally Posted by Obscurity View Post
    EDIT: Clarity, the version I sent you didn't have chance. Chance is not the chance that nothing will happen - it's the chance that it'll press a vertical key before a horizontal key - if it decides to press any at all.
    Whoops. I goofed
    Last edited by Clarity; 10-14-2014 at 01:46 AM.

  8. #8
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Clarity View Post
    Honestly didn't know that was there, I've never looked at SRL-5, no copying intended! If that's the case, can I add it to SRL-6?
    Is there really a need for it.. We usually just do if Random(x) = 0 then... or for your example if Random(1,x) = 1 then. So I really doubt that it would be a needed addition.
    Last edited by slacky; 10-14-2014 at 01:52 AM.
    !No priv. messages please

  9. #9
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

    Default

    Quote Originally Posted by Obscurity View Post
    The Mayor. Using yours, randomChance(2) would be a 33% chance, not a 50%. :P
    random(2) will return either 0 or 1, meaning it has a 50% chance of returning 0 and resulting true

  10. #10
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    Oh? I thought random(2) could return 0, 1, or 2, being 3 possibilities. Thanks for pointing that out.

  11. #11
    Join Date
    Dec 2011
    Posts
    2,147
    Mentioned
    221 Post(s)
    Quoted
    1068 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    Is there really a need for it.. We usually just do if Random(x) = 0 then... or for your example if Random(1,x) = 1 then. So I really doubt that it would be a needed addition.
    If people are very used to that way, then sure, it's redundant. I personally like the perceived convenience of this version, but if nobody cares then never mind.
    E: Looking at the above discussion it seems like it's not necessary anyway. Seems like I also misunderstood how random(2) works. I'll get back to my corner and work on includes, haha.
    Last edited by Clarity; 10-14-2014 at 02:01 AM.

  12. #12
    Join Date
    Aug 2014
    Location
    Australia
    Posts
    932
    Mentioned
    53 Post(s)
    Quoted
    495 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    It simply generates a random number in the range of [1..randomNumber] where randomNumber is the given number then it checks the generated number is 1.)
    Ahhh, I was unaware that random(int, int) could be used as a replacement for randomRange. That explains it.

    Thanks guys!



    New to scripting? Procedures & Functions for Beginners
    Do you use your computer at night? Just get f.lux

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
  •