How do you use Math.random to generate random ints?
My code is:
int abc= (Math.random()*100); System.out.println(abc);
All it prints out is 0, how can I fix this?
This question is tagged with
~ Asked on 2011-12-22 22:54:16
Cast abc to an integer.
~ Answered on 2011-12-22 22:55:17
For your code to compile you need to cast the result to an int.
int abc = (int) (Math.random() * 100);
However, if you instead use the java.util.Random class it has built in method for you
Random random = new Random(); int abc = random.nextInt(100);
~ Answered on 2011-12-22 23:01:01