/**
* min and max are to be understood inclusively
*/
public static int getRandomNumber(int min, int max) {
return (new Random()).nextInt((max - min) + 1) + min;
}
Random Number Generator in Android
If you want to know about random number generator in android then you should read this article till end. Here you can get all information about random number generator in android. Random Number Generator in Android
You should use this code in your java file.
Random r = new Random();
int randomNumber = r.nextInt(100);
tv.setText(String.valueOf(randomNumber));
I hope this answer may helpful for you. If you want to read more about this article then you should read this article. Random Number Generator
Below code will help you to generate random numbers between two numbers in the given range:
private void generateRandomNumbers(int min, int max) {
// min & max will be changed as per your requirement. In my case, I've taken min = 2 & max = 32
int randomNumberCount = 10;
int dif = max - min;
if (dif < (randomNumberCount * 3)) {
dif = (randomNumberCount * 3);
}
int margin = (int) Math.ceil((float) dif / randomNumberCount);
List<Integer> randomNumberList = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < randomNumberCount; i++) {
int range = (margin * i) + min; // 2, 5, 8
int randomNum = random.nextInt(margin);
if (randomNum == 0) {
randomNum = 1;
}
int number = (randomNum + range);
randomNumberList.add(number);
}
Collections.sort(randomNumberList);
Log.i("generateRandomNumbers", "RandomNumberList: " + randomNumberList);
}