+ 1
which is the code for picking a random number?
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.
+ 1
You have to use the Random package
0
Upvote for @James answer.
0
thank you