Skip to content Skip to sidebar Skip to footer

How To Generate A Random Number, Then Display It On Screen?

Ok, im fairly new to android but i have managed to teach myself the basics, i am making an app where you press a button , and a new screen opens and it shows a randomly generated n

Solution 1:

Android's documentation is excellent. Here's a hello world app:

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

Just change

tv.setText("Hello, Android");

to

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

and make sure to import the Math library (if you're using eclipse, hit Ctrl+Shift+O).


Solution 2:

below code return a value in Integer:-

    public static int randomBox() {

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

}

Solution 3:

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

it may be help for you


Solution 4:

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

Solution 5:

Here is your documentation for Random. Beyond that I'm not sure if you want to launch an Activity or update a TextView or what have you. However, I strongly recommend reading the documentation for Activity as well as common tasks in android and User Interface. These should help you understand what you are trying to do.


Post a Comment for "How To Generate A Random Number, Then Display It On Screen?"