Create random even number inside range?

Here is the format I adhere to:

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

So here is my code. I am trying to get a random even number from 1 to 100:

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

I saw one solution - multiply the number by 2, but this violates the intended range.

I was thinking of making a bunch of if statements to solve this, but it seems too complicated. Is there a relatively simple solution?

+5
source share
7 answers

Just generate a number from 0 to 49, and then multiply it by 2.

 Random rand = new Random(); int randomNum = rand.nextInt(100/2) *2; 

To do this in a range, simply add a range to:

 int randomNum = startOfRange+rand.nextInt((endOfRange-startOfRange)/2) *2; 

Note that startOfRange must be an even number or converted to an even number.

+8
source

Here is a tiny example of how to do it

 static Random rand = new Random(); public static void main(String[] args) { for(int i = 0;i<100;++i) System.out.println(generateEvenNumber(0, 100)); } private static int generateEvenNumber(int min, int max) { min = min % 2 == 1 ? min + 1 : min; // If min is odd, add one to make sure the integer division canยดt create a number smaller min; max = max % 2 == 1 ? max - 1 : max; // If max is odd, subtract one to make sure the integer division canยดt create a number greater max; int randomNum = ((rand.nextInt((max-min))+min)+1)/2; // Divide both by 2 to ensure the range return randomNum *2; // multiply by 2 to make the number even } 
+2
source

After getting a random number between 1 and 100 and multiply it by 2 , you can get number % 100 . Sort of:

 int random = rand.nextInt(100); random = (random * 2) % 100; 

This number method will always be less than 100 and even.

+1
source

Another approach (perhaps not the most optimal):

  • Create an array (or list) with all the even numbers between 1 and 100. (This is easy with a loop)
  • If you need a random even number, just take a random number from this array or list (using a random size or length).
+1
source

The best way to generate a random number from 2 to 1000

 Random random = new Random(); int Low = 2; int High = 1000; int randomNumber = random.nextInt(High-Low) + Low; if(randomNumber % 2 != 0){ randomNumber = randomNumber + 1; } 

https://www.google.com/search?q=sim+phone+details

+1
source

In Java 1.7 or later, I would use ThreadLocalRandom :

 import java.util.concurrent.ThreadLocalRandom; // Get even random number within range [min, max] // Start with an even minimum and add random even number from the remaining range public static int randEvenInt(int min, int max) { if (min % 2 != 0) ++min; return min + 2*ThreadLocalRandom.current().nextInt((max-min)/2+1); } // Get odd random number within range [min, max] // Start with an odd minimum and add random even number from the remaining range public static int randOddInt(int min, int max) { if (min % 2 == 0) ++min; return min + 2*ThreadLocalRandom.current().nextInt((max-min)/2+1); } 

The reason for using ThreadLocalRandom is explained here . Also note that the reason we added +1 to the ThreadLocalRandom.nextInt () input is to make sure max is included in the range.

+1
source
 private int getRandomNoOdd(int maxValue) {//min value is 0, including zero, max value can potentially be reached by randomness int random = 0; while (true) { random = (int) (Math.random() * (maxValue + 1)); if (random % 2 != 0) continue; break; } return random; } 
+1
source

Source: https://habr.com/ru/post/1236611/


All Articles