Given the some characters (AllCharacters
), you could pickup a character in the string randomly. Then use-for loop to get random charcter repeatedly.
public class MyProgram { static String getRandomString(int size) { String AllCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; StringBuilder sb = new StringBuilder(size); int length = AllCharacters.length(); for (int i = 0; i < size; i++) { sb.append(AllCharacters.charAt((int)(length * Math.random()))); } return sb.toString(); } public static void main(String[] args) { System.out.println(MyProgram.getRandomString(30)); }}
- Try it on the sandbox
- Also see other languages implement random string generator