Java Random create random String example 5

Description

Java Random create random String example 5


public class Main {
   private static final String ALPHA_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   public static void main(String[] args) {

      System.out.println(getAlphaNumeric(10));
      System.out.println(getAlphaNumeric(20));
   }/*from  ww w . j av a  2  s .  com*/

   public static String getAlphaNumeric(int len) {
      StringBuffer sb = new StringBuffer(len);
      for (int i = 0; i < len; i++) {
         int ndx = (int) (Math.random() * ALPHA_NUM.length());
         sb.append(ALPHA_NUM.charAt(ndx));
      }
      return sb.toString();
   }
}



PreviousNext

Related