I really want some Java that randomizes from 1-2 and all that I came up with (Doesn't work either) is:
random.math (int.1+2)
Might actually look stupid to an expert but yeah
I really want some Java that randomizes from 1-2 and all that I came up with (Doesn't work either) is:
random.math (int.1+2)
Might actually look stupid to an expert but yeah
Since you're just wanting to flip between two integers, You can utilize a ternary operator and the Math.random()
static method to achieve the desired results:
Math.random() >= 0.5 ? 2 : 1
The easiest would be to use the Random.nextInt(int n)
method, which will generate an integer between 0
and n-1
, inclusive.
Here is an example:
Random rnd = new Random();
for (int i = 0; i < 20; i++) {
int number = rnd.nextInt(2) + 1;
System.out.print(number + " ");
}
OUTPUT
1 2 1 1 2 1 1 2 2 1 2 2 2 2 1 2 2 2 1 1