+ 2
How can I do a random number
4 Réponses
+ 5
import java.util.Random;
Random rand = new Random();
int number = rand.nextInt(10)+1;
that would give you a random number between 1 and 10
+ 2
You could also use:
Math.random();
Which prints a double value between 0 and 1.
Example:
If you wanted a random between 0 and 100:
(int)(Math.random()*100)
@The One True Morty 's answer is more readable and remains true to je object oriented nature of Java so I'd suggest to go with that, but just in case you need a second answer.
+ 2
Here - A useful class in java dealing with random numbers.
https://docs.oracle.com/javase/8/docs/api/java/util/Random.html
+ 1
int randNumber;
Random random = new Random();
randNumber.nextInt(100);
System.Out.PrintIn(randNumber);
It would output a random number between 0 to 100