How can I create an app that generates a random number in Android using Eclipse and then show the result in a TextView
field? The random number has to be in a range selected by the user. So, the user will input the max and min of the range, and then I will output the answer.

- 37,901
- 21
- 84
- 115

- 1,187
- 4
- 12
- 20
11 Answers
To extend what Rahul Gupta said:
You can use Java function int random = Random.nextInt(n)
.
This returns a random int
in the range [0, n-1]
.
I.e., to get the range [20, 80]
use:
final int random = new Random().nextInt(61) + 20; // [0, 60] + 20 => [20, 80]
To generalize more:
final int min = 20;
final int max = 80;
final int random = new Random().nextInt((max - min) + 1) + min;

- 4,540
- 6
- 35
- 58

- 37,901
- 21
- 84
- 115
-
1can you take a look at this one: https://stackoverflow.com/q/46638945/6144372 It is related to `Random.nextInt(n)`. – Hammad Nasir Oct 09 '17 at 12:17
Random r = new Random();
int i1 = r.nextInt(45 - 28) + 28;
This gives a random integer between 28 (inclusive) and 45 (exclusive), one of 28,29,...,43,44.

- 3,770
- 2
- 34
- 37
-
9answer copied from here: https://stackoverflow.com/a/6029519/6144372 At least consider giving credit to the original answer. – Hammad Nasir Oct 09 '17 at 04:45
Also, from API level 21 this is possible:
int random = ThreadLocalRandom.current().nextInt(min, max);

- 2,065
- 3
- 21
- 32
-
8Returns the same number too frequently here. Bad experience! – 1111161171159459134 Jan 11 '19 at 02:40
-
-
1yes, indeed, I don't use this anymore either. The random class is better – larsaars Jun 30 '20 at 18:08
/**
* min and max are to be understood inclusively
*/
public static int getRandomNumber(int min, int max) {
return (new Random()).nextInt((max - min) + 1) + min;
}

- 1,664
- 2
- 15
- 19

- 3,789
- 1
- 27
- 25
" the user is the one who select max no and min no ?" What do you mean by this line ?
You can use java function int random = Random.nextInt(n)
. This returns a random int in range[0, n-1]).
and you can set it in your textview using the setText()
method

- 37,901
- 21
- 84
- 115

- 5,275
- 8
- 35
- 66
-
Error: Cannot make a static reference to the non-static method nextInt(int) from the type Random – Prasad Dec 29 '14 at 05:44
-
-
Your error is not because of my code. Show the class in which you have written this code. This error usually comes in a static method – Rahul Gupta Dec 29 '14 at 09:10
-
-
Post your code in which you are getting this issue and the line number also – Rahul Gupta Jan 02 '15 at 09:58
So you would want the following:
int random;
int max;
int min;
...somewhere in your code put the method to get the min and max from the user when they click submit and then use them in the following line of code:
random = Random.nextInt(max-min+1)+min;
This will set random to a random number between the user selected min and max. Then you will do:
TextView.setText(random.toString());

- 495
- 5
- 16
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

- 21
- 2
-
(From Review: The second link you posted is completely broken. Please edit your answer and either fix it OR remove it. Thank you.) – sɐunıɔןɐqɐp Jul 11 '18 at 07:16
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);
}

- 21
- 2
You can use If Random
. For example, this generates a random number between 75 to 100.
final int random = new Random().nextInt(26) + 75;

- 9,899
- 6
- 42
- 67

- 11
- 2
Use below function
public int generate_random(int min, int max) {
return new Random().nextInt((max - min) + 1) + min;
}

- 241
- 3
- 4
Just put that code in your java file
public static int generateRandomNumber(int min, int max) {
return new Random().nextInt((max- min) + 1) + min;
}

- 39
- 5