A random String id of specified length is generated as follows:
A set of valid characters is stored in a string.
From this set randomly 10 characters are selected and an Id is formed.
A set of valid characters is stored in a string.
From this set randomly 10 characters are selected and an Id is formed.
import java.util.Random;
public class XUIDGenerator
{
private static final int NUM_CHARS = 10;
private static String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static Random r = new Random();
public static String getUniqueID()
{
char[] buf = new char[NUM_CHARS];
for (int i = 0; i < buf.length; i++)
{
buf[i] = chars.charAt(r.nextInt(chars.length()));
}
return new String(buf);
}
public static void main(String[] args)
{
System.out.println(XUIDGenerator.getUniqueID());
}
}