Java – random number

I have a question: how to draw a number from 0 to 4 in Java (Android)? How to use random functions?

resolvent:

The following will do what you need

Random r = new Random();
int randomInt = r.nextInt(5);

If you do this in a loop, be sure to initialize random. Com outside the loop

Random r = new Random();
for(int i = 0;i < someThing; i++) {
    System.out.println(r.nextInt(5));
}

See the documentation for the random class: http://download.oracle.com/javase/6/docs/api/java/util/Random.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>