How to generate a random number and then display it on the screen?

Ok, I'm pretty new to Android, but I managed to teach myself the basics, I create an application where you press a button and a new screen opens and it shows a random number, the only problem is I don’t know how to generate and display random numbers , I searched the Internet for ages and found only small pieces of information, this assistant professor really makes sense to me.: /

If someone can help me or even give me some information that should guide me in the right direction, it would be great

EDIT: (for comments below)

    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("Random Number : " + Math.random());
    int random = (int)Math.ceil(Math.random()*100);
    setContentView(tv);

Here is the code that I have, where I made a mistake ^^^^: /

+3
source share
10

Android . hello world:

http://developer.android.com/guide/tutorials/hello-world.html

tv.setText("Hello, Android");

to

tv.setText("Random Number: " + Math.random());

Math ( eclipse, Ctrl + Shift + O).

+8

Integer: -

    public static int randomBox() {

    Random rand = new Random();
    int pickedNumber = rand.nextInt(100);
    return pickedNumber;

}
+5
Random rand = new Random();
String randomInt = list.get(rand.nextInt(list.size()));

+2
    Random r = new Random();

    StringBuffer temp = new StringBuffer("Random numbers:");
    for (int i = 0; i < 10; i++) {
        int i1 = r.nextInt(100 - 0) + 0;
        temp.append(String.valueOf(i1));
        temp.append(String.valueOf(" "));
    }
    return temp.toString();
+2

:

yourVariable = Math.random();

Android. 0 1. yourVariable TextView .setText(yourVariable), ...

+1

Random. , , Activity TextView . , Activity, android . , .

+1
int number = (new Random().nextInt(100));

, ,

0

If you want to develop your own algorithms for generating a random number, select any algorithm and implement it in any preferred language. https://en.wikipedia.org/wiki/List_of_random_number_generators

0
source

In a different way, but just :)

    Calendar c = Calendar.getInstance();
    final int seconds = c.get(Calendar.SECOND);

    Random r = new Random();
    final int n = r.nextInt(80 - 65) + 65;

    Toast.makeText(getApplicationContext(), ""+ n + seconds, Toast.LENGTH_SHORT).show();  
0
source
        Random r = new Random();
        rendomNumber = r.nextInt(80- 65) + 65;
        System.out.print(""+rendomNumber )
0
source

Source: https://habr.com/ru/post/1736026/


All Articles