How do I use Math.random() to generate a random integer between 1 and 99, both inclusive?

6

2 Answers

This works, make sure to import java.util.Random:

int max = 100; int min = 1; Random rand = new Random(); int randomNum = rand.nextInt((max - min) + 1) + min; 
0
int min =1; int max = 99; private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.