From a comment, it seems that you are generating group codes for your own application.
For the purposes and scale of your app, 5-character codes may be appropriate. But there are several points you should know:
- Random number generators are not designed to generate unique numbers. You can generate a random code as you're doing now, but you should check that code for uniqueness (e.g., check it against a table that stores group codes already generated) before you treat that code as unique.
- If users are expected to type in a group code, you should include a way to check whether a group code is valid, to avoid users accidentally joining a different group than intended. This is often done by adding a so-called "checksum digit" to the end of the group code. See also this answer.
- It seems that you're trying to generate codes that should be hard to guess. In that case,
Math.random()
is far from suitable (as is java.util.Random
) — especially because the group codes are so short. Use a secure random generator instead, such as java.security.SecureRandom
(fortunately for you, its security issues were addressed in Android 4.4, which, as I can tell from a comment of yours, is the minimum Android version your application supports; see also this question). Also, if possible, make group codes longer, such as 8 or 12 characters long.
For more information, see Unique Random Identifiers.
Also, there is another concern. There is a serious security issue if the 5-character group code is the only thing that grants access to that group. Ideally, there should be other forms of authorization, such as allowing only logged-in users or certain logged-in users—
- to access the group via that group code, or
- to accept invitations to join the group via that group code (e.g., in Google Classroom, the
PERMISSION_DENIED
error code can be raised when a user tries to accept an invitation to join a class).