Creating random numbers and storing in an array in Java

Posted By: Java Examples - 8:11 PM

Share

& Comment

Java version of this program is given below :
public class Random {
    public static int a[];
    public int l;
    public static int integer(int from, int to){
     return (int) Math.round(Math.random()*(to-from))+from;

    }
public void array(){
    int k=integer(2000,5000);
    a=new int[k];
    for(int i=0;i<k;i++){
        a[i]=integer(2000,5000);
    }
}
public int get_key(){
   int key=integer(2000,5000);
  int index=-1;
   for(int j=0;j<a.length;j++){
       if(a[j]==key) {
           index=j;break;
       }
   }
  return index;

}

public static void main (String args[]){
    Random n=new Random();
    n.array();
    System.out.println("Array length is ="+a.length+"....."                          +"Get the key index ="+n.get_key());
}

}


About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .