+ 1

which is the code for picking a random number?

16th Aug 2016, 9:22 PM
Peti Stoik
Peti Stoik - avatar
4 Antworten
+ 5
import java.util.Random; Two ways to go about using it. Can create a variable: Random rand = new Random(); To use it, do rand.nextInt(num), which will give you a number between 0 and number inside. Or: System.out.println((int)(Math.random() * 10)); Math.random() gives a double number between 0 and 1. Multiply it by a number you wish it random to, then typecast it as int if you want a whole number and no decimal.
16th Aug 2016, 11:23 PM
James
James - avatar
+ 1
You have to use the Random package
16th Aug 2016, 10:08 PM
Divjot Bedi
0
Upvote for @James answer.
17th Aug 2016, 12:26 AM
WPimpong
WPimpong - avatar
0
thank you
17th Aug 2016, 1:30 AM
Peti Stoik
Peti Stoik - avatar