Created
May 5, 2022 11:11
-
-
Save Koboo/5c805c1ad36782610dea3600f169ac8d to your computer and use it in GitHub Desktop.
Create a random string by a given length and characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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