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

后端 未结 9 1847
抹茶落季
抹茶落季 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:48

    " 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

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

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

    final int random = new Random().nextInt(26) + 75;
    
    0 讨论(0)
  • 2020-12-05 00:02
    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.

    0 讨论(0)
提交回复
热议问题