Skip to content

Instantly share code, notes, and snippets.

@Koboo
Created May 5, 2022 11:11
Show Gist options
  • Save Koboo/5c805c1ad36782610dea3600f169ac8d to your computer and use it in GitHub Desktop.
Save Koboo/5c805c1ad36782610dea3600f169ac8d to your computer and use it in GitHub Desktop.
Create a random string by a given length and characters
public class StringGenerator {
public static String createRandomString(int length, String characters) {
StringBuilder stringBuilder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (characters.length() * Math.random());
stringBuilder.append(characters.charAt(index));
}
return stringBuilder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment