开发者

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

开发者 https://www.devze.com 2022-12-22 02:54 出处:网络
Ok, im fairly n开发者_运维问答ew 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 number

Ok, im fairly n开发者_运维问答ew 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 number, the only problem is i dont know how to generate and display the random number, i have been searching the web for ages and have only found little snippets of information , that dosent really make sense to me. :/

If someone could help me , or even give me just a little bit of info that should guide me in the right direction it would be great

EDIT: (for the comment 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);

Thats the code i have , where have i gone wrong ^^^^ :/


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).


below code return a value in Integer:-

    public static int randomBox() {

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

}


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

it may be help for you


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


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.


Actually, you could easily use :

yourVariable = Math.random();

Should work in Android. Gives you a number between 0 and 1. Then you give yourVariable to a TextView with the method .setText(yourVariable) for instance...


If you are willing to devloper your own algorithms to generate random number, pick any algo and implement in any language of preference. https://en.wikipedia.org/wiki/List_of_random_number_generators


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

a random number will be assigned to variable number, each time it is used


Different way, but simple:)

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


        Random r = new Random();
        rendomNumber = r.nextInt(80- 65) + 65;
        System.out.print(""+rendomNumber )
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号