[Solved] Generate a random integer within a given range AND is even ONLY [closed]


If you wanted to be clever, you could make sure the least significant bit of the number is not set:

int num = (new Random().nextInt(199) & ~1) + 2;

This will ensure that the number is always even.

Thanks Eyal Shneider and Omaha


Marcelo‘s comment from the OP is the correct answer, though.

Get a number between 1-100, and multiple by 2:

int num = (new Random().nextInt(100) + 1) * 2;

13

solved Generate a random integer within a given range AND is even ONLY [closed]