Java : Random alphabets, alpha-numeric or numbers only string generator
A simple Java function to generate random string based on given parameter - alpha
, alphanum
and numeric
. Depending on the parameter type, this function will generate random string that is composed of alphabets, mixed of alphabets and numbers or just plain numbers only.
Here you go!
package socketloop;
import java.security.SecureRandom;
public class HumanReadablePasswordGenerator {
private static SecureRandom random = new SecureRandom();
private static final String ALPHA_NUMERIC_DICTIONARY = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final String ALPHA_DICTIONARY = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final String NUMERIC_DICTIONARY = "0123456789";
public static void main(String[] args) {
System.out.format("Random ALPHA NUMERIC string : %s%n", randStr(12,"alphanum"));
System.out.format("Random ALPHA string : %s%n", randStr(12,"alpha"));
System.out.format("Random NUMERIC string : %s%n", randStr(12,"numeric"));
}
public static String randStr(int strSize, String type) {
// sanity check
if (strSize < 1) throw new IllegalArgumentException();
// initialize
StringBuilder data = new StringBuilder(strSize);
char randomCharacter = 'a';
int character = 0;
while (strSize-- != 0) {
if (type == "alphanum") {
int characterLength = (int)(Math.random()*ALPHA_NUMERIC_DICTIONARY.length());
// sometimes using SecureRandom will introduce "bound must be positive" IllegalArgumentExecption here
if (characterLength > 0) {
character = random.nextInt(characterLength);
} else {
character = characterLength;
}
randomCharacter = ALPHA_NUMERIC_DICTIONARY.charAt(character);
}
if (type == "alpha") {
int characterLength = (int)(Math.random()*ALPHA_DICTIONARY.length());
// sometimes using SecureRandom will introduce "bound must be positive" IllegalArgumentExecption here
if (characterLength > 0) {
character = random.nextInt(characterLength);
} else {
character = characterLength;
}
randomCharacter = ALPHA_DICTIONARY.charAt(character);
}
if (type == "numeric") {
int characterLength = (int)(Math.random()*NUMERIC_DICTIONARY.length());
// sometimes using SecureRandom will introduce "bound must be positive" IllegalArgumentExecption here
if (characterLength > 0) {
character = random.nextInt(characterLength);
} else {
character = characterLength;
}
randomCharacter = NUMERIC_DICTIONARY.charAt(character);
}
data.append(randomCharacter);
}
return data.toString();
}
}
Sample output:
Random ALPHA NUMERIC string : 0M40310F4HR2
Random ALPHA string : ATdXEOYJJMIM
Random NUMERIC string : 103010200000
See also : Golang : Generate random string
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+25k Golang : Convert long hexadecimal with strconv.ParseUint example
+27.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+13.1k Golang : Count number of runes in string
+7.5k Golang : Getting Echo framework StartAutoTLS to work
+6.4k Golang : Warp text string by number of characters or runes example
+26.3k Golang : Encrypt and decrypt data with AES crypto
+5.1k Golang : Return multiple values from function
+6.4k Golang : Experimental emojis or emoticons icons programming language
+15.4k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+7.4k Golang : Convert(cast) io.Reader type to string
+5.1k Golang : Reclaim memory occupied by make() example
+18.9k Golang : Check if directory exist and create if does not exist