Math.random()
yields a random double number in the range zero (0.0) to one (1.0) (excluding the upper limit of exactly 1.0). You can print this value using: double rand = Math.random();You can convert this to an integer in the range 1 to n using the following code where n = 10
System.out.println(""+rand);
int n = (int)(Math.random()*10)+1;
System.out.println(""+n);
No comments:
Post a Comment