+ 2

How can I do a random number

9th Dec 2018, 4:58 PM
HenryHaven
HenryHaven - avatar
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
9th Dec 2018, 5:20 PM
LordHill
LordHill - avatar
+ 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.
9th Dec 2018, 6:20 PM
littlefrank
littlefrank - avatar
+ 2
Here - A useful class in java dealing with random numbers. https://docs.oracle.com/javase/8/docs/api/java/util/Random.html
11th Dec 2018, 11:39 AM
Seniru
Seniru - avatar
+ 1
int randNumber; Random random = new Random(); randNumber.nextInt(100); System.Out.PrintIn(randNumber); It would output a random number between 0 to 100
11th Dec 2018, 12:26 AM
Eromosele Jude Odanlumen