Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Tuesday, June 20, 2017

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;

0 Comments:

Post a Comment

Stats