Tuesday, 20 November 2012

Generate Random Number

There might be a need we could like to generate some random number to pass as test data to application, I wondered i could write a function so that each time it generates some random number so that i can run these scripts multiple times without worrying about the test data and guess what we can even pass the range whether you need single digit or two or digit or etc.



The below program illustrates random number generation within limit specified(10000).





 public static String getID(){

  int n=351;
 double d=0;          
 String Random;          
   int num=1;  
   {                    
while(true)          
{   
    
int final_limit=10000; //Specifiy the maximum limit          
d=Math.random()*final_limit;          
 num=(int)d;          
Random=String.valueOf(num);          
break;          
         
}
         
return Random;  

//This will return random number every time you run within 10000(specified limit)     
}   
    
}          


       

No comments:

Post a Comment