Last active
September 26, 2021 10:00
-
-
Save vigneshrcsengg/428d3bcd273ccf4cbecd19a3f54b4af0 to your computer and use it in GitHub Desktop.
Generate Random String
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.util.Random; | |
class GenerateRandomString { | |
public static void main(String args[]) { | |
System.out.println(getRandomeStr()); | |
} | |
public static String getRandomeStr() { | |
int leftLimit = 48; // numeral '0' | |
int rightLimit = 122; // letter 'z' | |
int targetStringLength = 7; | |
Random random = new Random(); | |
String generatedString = random.ints(leftLimit, rightLimit + 1) | |
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)) | |
.limit(targetStringLength) | |
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) | |
.toString(); | |
return generatedString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment