Random error, very random...

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

  1. 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?

  2. 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. 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. @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. 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. 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. Then there is something very wrong with your java installation.
  8. 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.

Share This Page