C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First: We call nextInt with an exclusive bound of 26. This yields the values 0 through (and including) 25.
Add: We add 97 to the values to adjust to the lowercase characters (97 is the ASCII code for lowercase A).
Caution: This code does not handle international (Unicode) characters. It just handles lowercase ASCII letters. It is limited.
Java program that generates random lowercase letters
import java.util.Random;
public class Program {
public static void main(String[] args) {
Random random = new Random();
// Generate 10 random lowercase letters.
for (int i = 0; i < 10; i++) {
// Max value is exclusive.
// ... So this returns 1, 2, through 25.
int n = random.nextInt(26);
// Add 97 to move from integer to the range A to Z.
char value = (char) (n + 97);
// Display our results.
System.out.println(value + "..." + Integer.toString(n));
}
}
}
Output
s...18
o...14
y...24
d...3
t...19
p...15
q...16
f...5
f...5
h...7