Creating random numbers and storing in an array in Java

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++){
public int get_key(){
   int key=integer(2000,5000);
  int index=-1;
   for(int j=0;j<a.length;j++){
       if(a[j]==key) {
  return index;


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


Popular posts from this blog

The n-queens problem in Java

Convert infix to postfix using stack in Java