Generate Random Number in Java

In java we use math class's methods and with little bit overriding it we can generate random numbers. There are two parameters in method one is starting point from where the random numbers will start generating and a range of last position (ending point).In this program we specified the range 10 to 100.

Source Code



public class Random_Numbers {
 public static int generate(int min,int max)
    {
        return min + (int)(Math.random() * ((max - min) + 1));
    }
 
 public static void main(String[] args)
 {
  System.out.println("\t \t \t LEP Tutorials \n ");
  for (int i =0;i<5;i++)
  {
  System.out.println("Random Numbers Generator :" + generate(10,100));
 }
  }
}

Output of the Program

Generate Random Number in Java
Generate Random Number in Java

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment