然后您可以编写代码: char randomletter = 'A' + (random() % 26);这不适用于 EBCDIC .您也可以尝试 char randomletter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[random () % 26];可与任何编码(包括 EBCDIC , ASCII , UTF-8 , ISO-Latin-1 一起使用em>)....其中每个 A ... Z 字母均由单个char编码. 但是这是一个有趣的答案,解释了为什么random() % 26是错误的.以我幼稚的非专家观点,这已经足够了.用我们在其中定义的myrandomlt26()替换random() % 26 static inline unsigned myrandomlt26() { long l; do { l = random(); } while (l>=(RAND_MAX/26)*26); return (unsigned)(l % 26);}应该稍微好一点(上面的do ... while循环通常应该运行一次).但是,请了解更多信息,例如关于 Unicode ,您会发现在某些人类语言中,字母的概念(或大写)并不像您所相信的那么琐碎.我不是这些问题的专家,但都市传说说,奇怪的是,法语œ不是字母(而是连字),因为在讨论ISO会议时,法国代表感到不舒服.我记得在法国一所学校里(大概在1966-1972年)得知这不是一封信(但不是手写手写笔),它的特殊名称​​"E dans l'O" 法语-是拼写错误),但有些人则认为相反.通常将带有重音符号的字母(如é或à)视为法语中的字母(但其UTF-8编码需要几个连续的char ....).同样,我相信并了解到,西里尔字母软件签名 ь不是不是字母,即使看起来像一个字母也是如此.定义阿拉伯语,希伯来语,韩语,切诺基,泰国 ...也应该很有趣,而且辩论无休无止... I'm new to programming in C and have been given the task of creating a 2D array that stores random letters from A-Z using the random () function. I know how to do random numbers.nextvalue = random ( ) % 10001;but am unsure on how exactly to create a random character. I know that you can use ASCII to create a random character using the range 65 - 90 in the ASCII table. Can anyone help me come up with a solution? I would greatly appreciate it. 解决方案 You should assume something about the character encoding of your system. Let's suppose it is ASCII or UTF-8 and let us restrict ourselves to the 26 upper-case letters ABC...XYZ of the latin alphabet.Then you could code: char randomletter = 'A' + (random() % 26);This won't work on EBCDIC. You could also try char randomletter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[random () % 26];which would work with any encoding (including EBCDIC, ASCII, UTF-8, ISO-Latin-1) ....where each A...Z letter is encoded by a single char.But this is an interesting answer explaining why random() % 26 is wrong. In my naive non-expert view, it is good enough.Replacing random() % 26 by myrandomlt26() where we have definedstatic inline unsigned myrandomlt26() { long l; do { l = random(); } while (l>=(RAND_MAX/26)*26); return (unsigned)(l % 26);}should be slightly better (Very often the do...while loop above should run once).However, learn more e.g. about Unicode, you'll discover that there are some human languages where the very notion of letter (or upper-case) is not as trivial as you believe.I'm not an expert on these matters, but urban legend says that strangely the French œ is not a letter (but just a ligature) because the French representative at some ISO meeting was sick when it was discussed. At French school, I recall having learned (probably in 1966-1972) that it is not a letter (but not hand-writing the ligature -which has a special name "E dans l'O" in French- was a spelling mistake), but some people believe the opposite. Usually accented letters like é or à are considered letters in French (but their UTF-8 encoding takes several consecutive char....). Likewise 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 matter of endless debates... 这篇关于在C中生成随机字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 09:04
查看更多