Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random alphanumeric code

Status
Not open for further replies.

relisys

Programmer
Mar 6, 2003
65
GB
Anyone got a nice "clean" simple solution to create a random 16 character alphanumeric string consisting of numbers, and upper and lowercase letters? Mines very untidy and far too large!

Cheers!

Relisys
 
Code:
class RandString{
	static String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	static String get(int len){
		
		StringBuffer buf = new StringBuffer(len);
		while(buf.length() < len){
			long n = Math.round(((Math.random() * 100)) % base.length());
			buf.append(base.charAt((int)n));
		}
		return buf.toString();
	}
}

That was a fun distraction, thanks [cheers]
-pete

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top