Created
October 28, 2024 16:30
-
-
Save muhdkhokhar/ee946098d2457aa5aee1ce526208bec4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.security.SecureRandom; | |
public class RandomStringGenerator { | |
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
private static final int STRING_LENGTH = 8; | |
private static final SecureRandom RANDOM = new SecureRandom(); | |
public static String generateRandomString() { | |
StringBuilder sb = new StringBuilder(STRING_LENGTH); | |
for (int i = 0; i < STRING_LENGTH; i++) { | |
int index = RANDOM.nextInt(CHARACTERS.length()); | |
sb.append(CHARACTERS.charAt(index)); | |
} | |
return sb.toString(); | |
} | |
public static void main(String[] args) { | |
String randomString = generateRandomString(); | |
System.out.println("Random String: " + randomString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment