http://www.technicalpage.net/search/label/SQL

>> Generate Random Numbers in Java/Selenium , Create an unique 10 digit phone number

Code to generate Random Number

To watch in YouTube click here


Below code will generate random numbers from 0 to 9 , let's see the code:

package pkgName;  //this is package name

import java.util.Random;  //You need to import this class

public class RanDomNum {  //this is class

public static void main(String[] args) {

  Random rand = new Random();
                for (int i = 0; i<10;i++){
  int  n = rand.nextInt(10);

  System.out.println("The random number is = "+n);

                }
}
}


OutPut:

The random number is = 6
The random number is = 0
The random number is = 3
The random number is = 8
The random number is = 5
The random number is = 7
The random number is = 4
The random number is = 5
The random number is = 1
The random number is = 6


To Generate 10 digit unique phone number.

package pkgName;

import java.util.Random;  //You need to import this class

public class RanDomNum {

public static void main(String[] args) {

String m[] = new String[10]  ;

  Random rand = new Random();
                for (int i = 0; i<10;i++){
  int  n = rand.nextInt(10);

  m[i]= Integer.toString(n); // Converting Integer to String

                }

 System.out.println("Phone number is : "+m[0]+m[1]+m[2]+m[3]+m[4]+m[5]+m[6]+m[7]+m[8]+m[9]);

}
}

OutPut:
Phone number is : 6476296041

No comments:

Post a Comment