skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Sunday, October 14, 2012

Java Random Alpha Numeric string generation

Posted by Raju Gupta at 10:00 PM – 0 comments
 
Generate a random alpha numeric string whose length is the number of characters specified. Characters will be chosen from the set of alpha-numeric characters. Count is the length of random string to create.

private static final String ALPHA_NUMERIC_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
public static String randomAlphaNumeric(int count) { 
 StringBuilder builder = new StringBuilder(); 
 while (count-- != 0) { 
  int character = (int)(Math.random()*ALPHA_NUMERIC_STRING.length()); 
  builder.append(ALPHA_NUMERIC_STRING.charAt(character)); 
 } 
 return builder.toString(); 
}


Labels: Random Function

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger