总是重复随机选择
Always repeating random pick
我这里有一个代码。目标是创建一个多项选择测验,其中的问题来自一个文本文件,并将随机选择显示。我可以做一个选择题,问题是当它生成时,已经选择和显示的问题会再次显示。此外,选择来自文本文件上的答案,但问题是选择在一个问题中反复显示。
所以我想知道如何不再次显示选择问题,以及如何不重复显示选项。
示例输出
1. what month do we celebrate Christmas?
a.december
b.december
c.january
d.december
//Randomizes the questionList vector
random_shuffle(questionList.begin(), questionList.end());
//Goes through every Test question
for(int i = 0; i < questionList.size(); i++){
vector <Test> randomAnswer;
//Puts the correct Answer into it first
randomAnswer.push_back(questionList[i]);
//Then randomly gets 3 other answers from questionList
while(randomAnswer.size() < 4)
{
int random = rand();
if(random != i){
randomAnswer.push_back(questionList[rand() % (questionList.size() - 1)]);
}
//Shuffle the answers
random_shuffle(randomAnswer.begin(), randomAnswer.end());
//Print the question
cout << questionList[i].getQuestion() << ":" << endl;
//Initialize the first choice character to 'A'
char ch = 'A';
//Prints the shuffled answers
for(int j = 0; j < randomAnswer.size(); j++)
{
cout << ch << ") " << randomAnswer[j].getAnswer() << endl;
//Increment 'A' so it can print 'B' and so forth
++ch;
}
//Get users response
cout << "nYour answer: ";
cin >> response;
//Bool data type to determine if the correct answer was found
bool isCorrect = false;
switch(toupper(response))
{
case 'A':
if(randomAnswer[0].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'B':
if(randomAnswer[1].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'C':
if(randomAnswer[2].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'D':
if(randomAnswer[3].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
default:
cout << "nIncorrect input.n";
}
//If the answer was found print "Correct" else "Wrong"
if(isCorrect)
{
cout << "nYou got the answer correct!n";
}
else
{
cout << "nYou got the answer WRONG!n"
<< "Correct answer was " << questionList[i].getAnswer() <<
endl;
您的方法是不正确的,您将正确的答案插入列表中,然后随机选择三个答案。可视化示例(1是正确的):
1 2 3 4
Insert 1 into randomAnswer
Randomly pick three numbers and insert them into randomAnswer: ex (2, 1, 3)
正确的方法是把所有四个答案都洗牌。Psuedocode
swap(questionList[i], *(questionList.end() - 1));
Insert questionList[i]
Shuffle questionList.begin() - questionList.end() - 2 and randomly pick 3
相关文章:
- 随机选择 2 个不在范围内的整数
- 在C++中随机选择子类
- 如何从图形的邻接列表表示中随机选择边缘
- C++ 随机选择标准::矢量<标准::矢量的非空元素> >
- 以最新的C 的类型安全方式从枚举中随机选择元素
- C 通过随机选择其元素从两个元组制成元组
- 最佳的c++方式来随机选择设置位在位集中的位置
- 随机选择一个 0 到 8 之间的数字,直到没有数字可以选择,然后重新开始
- 有什么方法可以制作一个程序,随机选择10-20个字符串并用C++向用户显示
- 为连接的OMNeT++门分配随机选择但匹配的参数
- 从STL矢量的真值中随机选择索引
- 添加函数数组并随机选择其中一个
- 随机选择两个值
- 如何存储需要随机选择和删除的数据,矢量,哈希表或其他
- 就地随机选择算法
- 随机选择字典中的单词
- 从包含n个元素的向量中随机选择m个元素
- 如何在给定概率下随机选择数字
- 一个有效的方法,随机选择std::vector的所有元素正好一次而不重新洗牌
- 从文本文件中随机选择K行