Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Random error, very random...

dan

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

Code:
int randomDoor = 0;
randomDoor = Random.nextInt(1, 4);

Every time I start my script it will only generate 1 as the number, every single time. As its not a huge issue that stops my script 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.
 
Joined
Jan 8, 2015
Messages
1,426
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:
public static int getRandom(int from, int to) {
    if (from < to) return from + new Random().nextInt(Math.abs(to - from));
    return from - new Random().nextInt(Math.abs(to - from));
}
 

dan

Joined
Jan 15, 2015
Messages
60
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:
public static int getRandom(int from, int to) {
    if (from < to) return from + new Random().nextInt(Math.abs(to - from));
    return from - new Random().nextInt(Math.abs(to - from));
}

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.
 
Mod Automation
Joined
Jul 26, 2013
Messages
3,053
@Cloud this is the weirdest bug report I've seen in a while. We're using a pretty standard pseudo-random number generator right?
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
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.
 

dan

Joined
Jan 15, 2015
Messages
60
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.

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 over a dozen time and every time it was returning 1 for the first generated number, and 2 for the second.
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
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 over a dozen time and every time it was returning 1 for the first generated number, and 2 for the second.
Then there is something very wrong with your java installation.
 
Mod Automation
Joined
Jul 26, 2013
Messages
3,053
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 over a dozen time and every time it was returning 1 for the first generated number, and 2 for the second.
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.
 
Top