I have this class which is used as part of a game. It needs to generate Random Even values, which is done by generating random numbers until the result is even.
Is there a better way to do this?
Also, I currently have the methods implemented as private instance methods, but should I declare generateRandomNumber()
and evenNumber(number)
as static methods? Will it have any benefits?
public class Game {
//...
public void opponentSaysEvenNumber() {
int number = generateRandomEvenNumber();
System.out.println("Opponent: " + number);
}
private int generateRandomEvenNumber() {
Random random = new Random();
int number = random.nextInt();
while (!evenNumber(number)) {
number = random.nextInt();
}
return number;
}
private boolean evenNumber(int number) {
return (number % 2) == 0;
}
}