How can I generate a random number in a certain range?

后端 未结 9 1846
抹茶落季
抹茶落季 2020-12-04 23:25

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 r

相关标签:
9条回答
  • 2020-12-04 23:41

    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;
    
    0 讨论(0)
  • 2020-12-04 23:44
    private int getRandomNumber(int min,int max) {
        return (new Random()).nextInt((max - min) + 1) + min;
    }
    
    0 讨论(0)
  • 2020-12-04 23:46

    Also, from API level 21 this is possible:

    int random = ThreadLocalRandom.current().nextInt(min, max);
    
    0 讨论(0)
  • 2020-12-04 23:46

    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

    0 讨论(0)
  • 2020-12-04 23:46

    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);
    }
    
    0 讨论(0)
  • 2020-12-04 23:47

    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());
    
    0 讨论(0)
提交回复
热议问题