I'm guessing this is very simple, but for some reason I am unable to figure it out. So, how do you pick a random integer out of two numbers. I want to randomly pick an integer out of 1 and 2.
Just use the standard uniform random distribution, sample it, if it's less than 0.5 choose one value, if it's greater, choose the other:
int randInt = new Random().nextDouble() < 0.5 ? 1 : 2;
Alternatively, you can use the nextInt
method which takes as input a cap (exclusive in the range) on the size and then offset to account for it returning 0 (the inclusive minimum):
int randInt = new Random().nextInt(2) + 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With