[android] How can I generate a random number in a certain range?

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.

This question is related to android random numbers range generator

The answer is


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


Also, from API level 21 this is possible:

int random = ThreadLocalRandom.current().nextInt(min, max);

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);
}

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());

" 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


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.


You can use If Random. For example, this generates a random number between 75 to 100.

final int random = new Random().nextInt(26) + 75;

private int getRandomNumber(int min,int max) {
    return (new Random()).nextInt((max - min) + 1) + min;
}

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to random

How can I get a random number in Kotlin? scikit-learn random state in splitting dataset Random number between 0 and 1 in python In python, what is the difference between random.uniform() and random.random()? Generate random colors (RGB) Random state (Pseudo-random number) in Scikit learn How does one generate a random number in Apple's Swift language? How to generate a random string of a fixed length in Go? Generate 'n' unique random numbers within a range What does random.sample() method in python do?

Examples related to numbers

how to display a javascript var in html body How to label scatterplot points by name? Allow 2 decimal places in <input type="number"> Why does the html input with type "number" allow the letter 'e' to be entered in the field? Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array Input type "number" won't resize C++ - how to find the length of an integer How to Generate a random number of fixed length using JavaScript? How do you check in python whether a string contains only numbers? Turn a single number into single digits Python

Examples related to range

How does String substring work in Swift Creating an Array from a Range in VBA How to create range in Swift? Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3? How to find integer array size in java How does one make random number between range for arc4random_uniform()? Skip over a value in the range function in python Excel Define a range based on a cell value How can I generate a random number in a certain range? Subscript out of range error in this Excel VBA script

Examples related to generator

Gradient text color Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables? Convert generator object to list for debugging How can I generate a random number in a certain range? Display SQL query results in php What does yield mean in PHP? How does C#'s random number generator work? How to len(generator()) How to take the first N items from a generator or list? How to pick just one item from a generator?