My question is: How to draw a number from 0 to 4 in Java (android)? How to use the random function?
The following will do what you need.
Random r = new Random(); int randomInt = r.nextInt(5);
If you do this in a loop, make sure you initialize Randomout of loop.
Random
Random r = new Random(); for(int i = 0;i < someThing; i++) { System.out.println(r.nextInt(5)); }
See the documentation for the class Random: http://download.oracle.com/javase/6/docs/api/java/util/Random.html
Be careful, as you should not create a new random object every time you want to get a new number. This line should be run once at application startup:
Random r = new Random();
, :
int x = r.nextInt(5);
System.out.println(""+(int) (Math.random()*5.0));
, , Math.random(), .
: 0 n, (0 , n ), :
(int) (Math.random()*n);
m m + n, (m , m + n ), :
m + (int) (Math.random()*n);
, Attantion - :
, ( , phanamona).
, - Java : Java: Can (new Random()). nextInt (5) ? : Java random , ?
:
static Random sRandomGen = new Random();
nextInt , . , .
int rnd = sRandomGen.nextInt(numofmatches - 1);
.
Source: https://habr.com/ru/post/1779251/More articles:Drupal: reusing faceted search blocks - drupalFunction declaration and implementation - c ++"Undefined variable: _SESSION" VS. "The session has already begun" - phpWhich Ruby analyzer do you suggest analyzing Ruby sources? - ruby | fooobar.comC # What is an efficient way to do this php style code "eval" - variable-assignmentIncluding Grails plugin source in Grails 1.3.5 project - grailsC # exception throwing - c #Django post_syncdb signal handler not getting a call? - pythonHow to use .NET CommandArgument as a non-string object? - c #How to create multiple templates for Java files in Eclipse? - eclipseAll Articles