Showing posts with label How to generate random numbers in Java program. Show all posts
Showing posts with label How to generate random numbers in Java program. Show all posts

Wednesday, 22 October 2014

Generate Random Number in Java

Structure of the Problem Requirements 

In this problem we will learn how to generate random numbers in Java Program. Before this we uploaded a program how to generate random numbers in C++ program. 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 between 10 to 100. Here is the source code of this problem which help you in better understanding.

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