Friday 20 January 2017

Java Program To Generate Random Numbers

Code:

import java.util.Random;
public class Rand_no 
{
public static void main(String[] args)
{
Random r=new Random();
int rand_nmbr1=r.nextInt();                     // gives random no. uptil integer range
  int rand_nmbr2=r.nextInt(100);               // 0 to 99
  double rand_nmbr3=r.nextDouble();       //gives random no. uptil the range of double
                     
  System.out.println("Integers Random number1 is = "+rand_nmbr1);
  System.out.println("0 To 99 Random number2 is = "+rand_nmbr2);
  System.out.println("Doubles Random number3 is = "+rand_nmbr3);
// if u want to generate 1 to 100 then
int n=r.nextInt(100)+1; // 1 to 100
System.out.println("Random number Betweem 1-100 is = "+n);
//A random number between 4 and 17 inclusive?
int ran = r.nextInt(14) + 4;
System.out.println("Random number between 4-17 = "+ran);
int n2=r.nextInt(7)+4;// Arbitrary Range or 4-10
System.out.println("Random number(4 to 7) Arbitrary range is = "+n2);
//A random number between 50 and 100 inclusive?
int n3=r.nextInt(51)+50;
System.out.println("Random number 50-100 = "+n3);
}
}   

Output:


No comments:

Post a Comment