how to generate random letters 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: create a random char java
import java.util.Random;
public class RandomChar {
public static void main(String[] args) {
Random random = new Random();
char randomizedCharacter = (char) (random.nextInt(26) + 'a');
System.out.println("Generated Random Character: " + randomizedCharacter);
}
}