Generate random number in java
The good approach is Random class
which has three methods
nextInt(); Generate a random integer
nextInt(max); Generate a random integer with range 0,max
nextDouble() Generate a random double number
For this first we need to create object of random class and then invoke methods
Random ab=new Random();
int ran=ab.nextInt();
Formula to generate number within range
ab.nextInt(high-low+1)+low;
which has three methods
nextInt(); Generate a random integer
nextInt(max); Generate a random integer with range 0,max
nextDouble() Generate a random double number
For this first we need to create object of random class and then invoke methods
Random ab=new Random();
int ran=ab.nextInt();
Formula to generate number within range
ab.nextInt(high-low+1)+low;
0 Comments:
Post a Comment