I have an Android app that uses randomly generated tokens to log in. They're 5 characters long, are sent to a user's email address, and have a short lifespan, so I don't think I need anything that's extremely secure.
I'm getting odd result that suggest I'm not really generating random numbers. That, or I'm seeing patterns that don't really exist.
For example, the last few tokens generated were:
- 0pqrH
- UMOPQ
- d789r
- jmnde
These have patterns:
- pqr
- 789
- all caps
- all lowercase
There are some tokens generated with scattered characters:
- f80!#
- NLog#
- Z6kpJ
It just seems that there are too many patterns to be coincidence.
static public String getToken(int chars) {
String CharSet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ234567890!@#$";
String Token = "";
for (int a = 1; a <= chars; a++) {
Token += CharSet.charAt(new Random().nextInt(CharSet.length()));
}
return Token;
}
Am I generating random numbers correctly? Should I be picking a seed? Is this in my imagination?