I faced some difficulties to figure out how to generate unique string within [1;200) range length. The code I've come up with attached below:
public static String generateRandString() {
String STRING_TOKENS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
while(stringBuilder.length() <= 200) {
int index = (int) random.nextFloat() * STRING_TOKENS.length();
stringBuilder.append(STRING_TOKENS.charAt(index));
}
return stringBuilder.toString();
}
The Problem:
Requesting 20 generated strings returns every time "AAAAAAAA" string probably of 200 symbols length
Expected output:
A7898as7sd6as5da
as87asd67
768asjhg435GhA900324
2g2j3h4gjhgAKL*78a9dd879234
3B234
1
Some limitations:
- No additional libraries (Google Guava or Apache Common)
- JDK 1.6 only
Many thanks to your contribution!