how to generate random string number in java code example
Example 1: generate random string in java
import java.util.Random;
public class Generator {
public static String generateRandomPassword(int len) {
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk"
+"lmnopqrstuvwxyz!@#$%&";
Random rnd = new Random();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
sb.append(chars.charAt(rnd.nextInt(chars.length())));
return sb.toString();
}
}
Example 2: java random string
public static String getRandomString(int size) {
String rand = "";
String chars = "1234567890-=!@#$%^&*()_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?";
for (int i = 0; i < size; i++) {
rand += chars.toCharArray()[new Random().nextInt(chars.length())];
}
return rand;
}