C语言 c语言生成随机字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19724346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
generate random characters in c
提问by Nick
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.
我是 C 编程的新手,并被赋予了创建一个二维数组的任务,该数组使用 random () 函数存储来自 AZ 的随机字母。我知道如何做随机数。
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.
但我不确定如何准确地创建一个随机字符。我知道您可以使用 ASCII 使用 ASCII 表中的 65 - 90 范围创建随机字符。谁能帮我想出一个解决方案?我将不胜感激。
回答by Basile Starynkevitch
You should assume something about the character encoding of your system. Let's suppose it is ASCIIor UTF-8and let us restrict ourselves to the 26 upper-case letters ABC...XYZof the latin alphabet.
您应该对系统的字符编码有所了解。假设它是ASCII或UTF-8,让我们将自己限制在拉丁字母表中的 26 个大写字母ABC...XYZ。
Then you could code:
然后你可以编码:
char randomletter = 'A' + (random() % 26);
This won't work on EBCDIC. You could also try
这不适用于EBCDIC。你也可以试试
char randomletter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[random () % 26];
which would work with any encoding (including EBCDIC, ASCII, UTF-8, ISO-Latin-1) ....where each A...Zletter is encoded by a single char.
它适用于任何编码(包括EBCDIC、ASCII、UTF-8、ISO-Latin-1)......其中每个A...Z字母由单个char.
But thisis an interesting answer explaining why random() % 26is wrong. In my naive non-expert view, it is good enough.
但是,这是一个有趣的答案,解释为什么random() % 26是错的。在我幼稚的非专家看来,这已经足够了。
Replacing random() % 26by myrandomlt26()where we have defined
替换random() % 26为myrandomlt26()我们定义的位置
static 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...whileloop above should run once).
应该稍微好一点(通常上面的do...while循环应该运行一次)。
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.
但是,了解更多信息,例如Unicode,您会发现在某些人类语言中,字母(或大写)的概念并不像您认为的那么简单。
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 severalconsecutive char....). Likewise I believe and have learned that the cyrillic soft-signьis nota 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...
我不是这些问题的专家,但都市传说说,奇怪的是法语?不是字母(而只是连字),因为在某个 ISO 会议上的法国代表在讨论时生病了。在法语学校,我记得我知道(可能是在 1966-1972 年)它不是一个字母(但不是手写连字——在法语中有一个特殊的名字“E dans l'O”——是一个拼写错误) ,但也有人认为相反。通常重音字母像é或被à认为是法语字母(但它们的 UTF-8 编码需要几个连续的char......)。同样,我相信并了解到,西里尔软迹象ь是不是一封信,即使它看起来像一封。定义什么是阿拉伯语、希伯来语、韩语、切诺基语、泰语中的字母……也应该很有趣,而且是无休止的辩论……
回答by Timmay
I think you have already solved this problem. You just need to think harder and tinker more with it. In C you can easily display any ASCII character in Dec form. Here is the ASCII Tablefor reference. For example
我想你已经解决了这个问题。你只需要更努力地思考和修补它。在 C 中,您可以轻松地以 Dec 形式显示任何 ASCII 字符。这里是ASCII 表供参考。例如
char c = 65;
char c = 'A'; // Both are the same thing.
So it is probably the same as generating a random number between 65 and 90. Hope this helps ;) Am also new to C with some background in C++.
所以它可能与生成 65 到 90 之间的随机数相同。希望这会有所帮助;) 我也是 C 的新手,有一些 C++ 背景。

