0

I have problems getting random numbers to stay within a certain range. I've tried a few different suggestions, but none of them worked so far. This is the latest one I tried. Using this code generates me numbers between 0-30. But I wanna stay within 50-20.

Suggestions?

 void randomNumbersForEquation() {

        Random randomNumbers = new Random();
        int number1 = randomNumbers.nextInt(51 - 20);
        int number2 = randomNumbers.nextInt(51 - 20);
        textEasyNumber1.setText("" + number1);
        textEasyNumber2.setText("" + number2);

    }
Fisnik B.
  • 125
  • 8
  • 2
    [This](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range) will help you. – Sangbok Lee Mar 06 '17 at 13:57
  • ... ahem, ... add 20? – Henry Mar 06 '17 at 13:59
  • @Henry, that doesn't help as all it does is to increase the range from 30-0 to 50-0 again. I don't want that. I want the numbers to be random between 50-20. – Fisnik B. Mar 06 '17 at 14:18
  • @SangbokLee I've seen some of these examples but I just can't get it to work. Still throws me out the wrong numbers. I was hoping someone would post a proper working code. – Fisnik B. Mar 06 '17 at 14:18
  • @FisnikB. low: 0+20 = 20, high: 30+20 = 50. Isn't that exactly what you want? – Henry Mar 06 '17 at 14:58
  • `int number1 = randInt.nextInt(50-20) + 20;` <<--- This was the solution and I assume this is what you suggested as well. Thanks for the help it works perfectly fine now! :). (I tried it earlier, but I assume I messed up something ex. wrongly initializing it, which prevented it from working). – Fisnik B. Mar 06 '17 at 15:11

0 Answers0