1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Random error, very random...

Discussion in 'Developer Support' started by Dan, Feb 6, 2015.

  1. Dan

    Dan

    Joined:
    Jan 15, 2015
    Messages:
    60
    Likes Received:
    9
    Currently in my script bot I have the following code to create a random variable between 1-4

    Code (Text):
    1. int randomDoor = 0;
    2. randomDoor = Random.nextInt(1, 4);
    Every time I start my script bot it will only generate 1 as the number, every single time. As its not a huge issue that stops my script bot running, I have never noticed until now that it doesn't seem to be generating different numbers. The next random number with same code, different name; Also returns a single number every time. 2.

    Is there a simple explanation for this? Or have I had a drink too many tonight?

    Thanks.
     
  2. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,429
    Likes Received:
    252
    Got any other code where this is implemented? Also, I don't think you have to initialize it before using random next int.
    Perhaps you can use something like Random.nextInt(1000,4000) and divide it by 1000 round down.

    Otherwise, I found this

    Code (Text):
    1. public static int getRandom(int from, int to) {
    2.     if (from < to) return from + new Random().nextInt(Math.abs(to - from));
    3.     return from - new Random().nextInt(Math.abs(to - from));
    4. }
     
  3. Dan

    Dan

    Joined:
    Jan 15, 2015
    Messages:
    60
    Likes Received:
    9
    Yea, just playing with the code I found it would generate random at with a higher max, but when it was set to 4, it was always 1. Odd, but yea, there is a workaround.
     
  4. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,938
    Likes Received:
    1,266
    @Cloud this is the weirdest bug report I've seen in a while. We're using a pretty standard pseudo-random number generator right?
     
  5. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    That code will generate a random value between 1 (inclusive) and 4 (exclusive). Its uses a secure random number generator. I'm assuming it was just a coincidence.
     
  6. Dan

    Dan

    Joined:
    Jan 15, 2015
    Messages:
    60
    Likes Received:
    9
    I thought that it may have been coincidence, but after testing it thoroughly I am 100% certain that it was not a coincidence. I ran the script bot over a dozen time and every time it was returning 1 for the first generated number, and 2 for the second.
     
  7. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    Then there is something very wrong with your java installation.
     
  8. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,938
    Likes Received:
    1,266
    Perhaps you could make a test bot that prints randomly generated numbers in a loop. Isolating and testing the issue may have different results. Additionally, if it doesn't you can pass the standalone bot our way and we can attempt to reproduce it.
     
  9. Exia

    Joined:
    Nov 3, 2013
    Messages:
    609
    Likes Received:
    259

Share This Page

Loading...