(You will want to take a look at the Random class in java. It provides random numbers, which is very useful.
To answer your question, I would suggest having a String of allowed characters in the ID, and constructing a new string using a StringBuilder, by selecting random characters from your ALLOWED_CHARACTERS string.
Something like
private static final String ALLOWED_CHARACTERS = "ABCD...789";
private static final Random RNG = new Random();
public static String getID() {
StringBuilder builder = new StringBuilder();
while (builder.length() < DESIRED_LENGTH) {
builder.append(ALLOWED_CHARACTERS.charAt((int)(RNG.nextDouble()*ALLOWED_CHARACTERS.length())));
}
return builder.toString();
}
Also, while unlikely, you might get some duplicates. If you really want to prevent duplicates, you can use a HashSet of String to see if your ID is already used. By putting your ID’s into a HashSet, you can efficiently check if an ID has already been generated.
solved Generate a RANDOM id for users [closed]