C++ 如何在c ++中选择随机字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20132650/
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
How to select random letters in c++
提问by user2340686
Hi I'm working on an hangman project for my final in my c++ class. I've got most of it working but I wanted to make it so the player could have the option to play against a computer opponent in single player mode. I tried doing something like 97+rand()%123 so then convert the number to characters but I keep getting weird characters like an upside down f I checked to see if I was missing something but I have the right directives and I included a srand. a simplified version of what I did looks like this
嗨,我正在为我的 C++ 课程的期末考试做刽子手项目。我已经完成了大部分工作,但我想让它让玩家可以选择在单人游戏模式下与电脑对手对战。我尝试做类似 97+rand()%123 这样的事情,然后将数字转换为字符,但我不断收到奇怪的字符,例如颠倒的 f 我检查了是否遗漏了某些东西,但我有正确的指令,并且我包含了一个 srand . 我所做的简化版本看起来像这样
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
char cch;
char ch;
unsigned seed= time(0)
srand(seed)
cch=97rand()%123;
ch=cch;
cout<<"computer chose "<< ch<<endl;
}
please note that I didn't include everything my project
请注意,我没有包括我的项目的所有内容
回答by beardhatcode
Use
用
cch = 'a' + rand()%26;
This will add a random number to 'a' (which in int equals 97 but using that number makes your code less readable), remember 'a' == 97
because C and C++ don't actually care about your data type.
这将向 'a' 添加一个随机数(在 int 中等于 97,但使用该数字会使您的代码可读性降低),请记住,'a' == 97
因为 C 和 C++ 实际上并不关心您的数据类型。
So if it is stored as char is will be seen as char
所以如果它存储为 char 将被视为 char
回答by Dragos
回答by Martyn Shutt
You may find it easier to define a char array with all the valid characters you want to choose from, then pick a random character between 0 and the number of characters stored in the array;
你可能会发现定义一个包含所有你想要选择的有效字符的字符数组更容易,然后在 0 和数组中存储的字符数之间随机选择一个字符;
char letters[] = "abcdefghijklmnopqrstuvwxyz";
char x = letters[rand() % 26];
It may not be as efficient, but it makes your intentions much clearer.
它可能没有那么高效,但它使您的意图更加清晰。
Also, if you ever needed to modify the probability of certain characters appearing more often, you could do so by adding the same character multiple times.
此外,如果您需要修改某些字符更频繁出现的概率,您可以通过多次添加相同的字符来实现。
回答by Aakash Goyal
The formula should be : cch = 97 + rand()%26;
公式应该是: cch = 97 + rand()%26;
回答by bames53
You don't want the computer to keep guessing the same letters over and over.
您不希望计算机一遍又一遍地猜测相同的字母。
#include <random>
#include <iterator>
#include <iostream>
#include <algorithm>
int main() {
char letters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::shuffle(std::begin(letters), std::end(letters),
std::mt19937(seed));
for (char c : letters) {
std::cout << "Computer chose: " << c << '\n';
}
}