I'm trying to make a program to generate a random account name for the user. The user will hit a button and it will copy the account name to his clipboard. The GUI part of it is working but I just can't think of the best way to handle random generation of a String.
Allowed characters in the username: A-Z a-z _
No numbers, no other symbols, and no two of the same character in a row can occur.
Must be of length six.
My idea:
create an array of characters:
[ _, a, b, c, d ... etc ]
Generate a random integer between 0 and array.length - 1
and pick the letter in that slot.
Check the last character to be added into the output String,
and if it's the same as the one we just picked, pick again.
Otherwise, add it to the end of our String.
Stop if the String length is of length six.
Is there a better way? Perhaps with regex? I have a feeling the way I'm thinking of doing it here is really bad.
Aucun commentaire:
Enregistrer un commentaire