lundi 23 novembre 2015

Generate a random even number between a range?

Here's the format I'm following:

 int randomNum = rand.nextInt((max - min) + 1) + min;

So here's my code. I'm trying to get a random even number between 1 and 100:

 Random rand = new Random(); 
 int randomNum = rand.nextInt((100 - 2) + 1) + 2;

I saw one solution is to multiply the number by 2, but that breaches the intended range.

I thought of doing a bunch of if statements to solve this, but it seems overly complex... Is there a relatively simple solution?




Aucun commentaire:

Enregistrer un commentaire