You should accept something about the character encoding of your system. Suppose it is ASCII or UTF-8 and let's limit ourselves to 26 uppercase letters ABC ... XYZ of the Latin alphabet.
Then you can enter the code:
char randomletter = 'A' + (random() % 26);
This will not work on EBCDIC . You can also try
char randomletter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[random () % 26];
which will work with any encoding (including EBCDIC, ASCII, UTF-8, ISO-Latin-1) .... where each letter A ... Z is encoded with one char .
But this one is an interesting answer explaining why random() % 26 is incorrect. In my naive non-expert look, this is good enough.
Replacing random() % 26 with myrandomlt26() , where we defined
static inline unsigned myrandomlt26() { long l; do { l = random(); } while (l>=(RAND_MAX/26)*26); return (unsigned)(l % 26); }
should be a little better (very often the do ... while above should run once).
However, learn more, for example. about Unicode , you will find that there are some human languages ββwhere the very concept of a letter (or uppercase) is not as trivial as you think.
I am not an expert on these issues, but the urban legend says that strange French is not a letter (but only a ligature), because the French representative at some ISO meeting was ill when he was discussed. In a French school, I remember that when I found out (possibly in 1966-1972) that this was not a letter (but not a letter-letter from the ligature, which in French has the special name "E dans l'O"), it was a spelling mistake ), but some people believe the opposite. Usually accented letters of type Γ© or Γ are considered letters in French (but their UTF-8 encoding takes several consecutive char ....). In the same way, I believe and have learned that the Cyrillic soft sign is not a letter, even if it looks like one. Defining what is a letter in arabic , hebrew , korean , cherokee , thai ... should also be fun and the stuff of endless debate ...