如何随机排列数组问题

How to shuffle array questions?

本文关键字:问题 数组 排列 何随机      更新时间:2023-10-16

我只是C++新手。当我出手时,有人可以帮我随机化这个问题吗?有没有办法随机化这个?

我有这个代码。有人可以告诉我如何随机化这些吗?

string questionpart[20]={"What is the square root of 4?",
                            "What is the square root of 6?",
                            "What is the square root of 16?",
                            "What is the square root of 25?",
                            "What is the square root of 36?",
                            "What is the square root of 42?",
                            "What is the square root of 48?",
                            "What is the square root of 81?",
                            "What is the square root of 100?",
                            "What is the square root of 121?",
                            "What is the square root of 144?",
                            "What is the square root of 169?",
                            "What is the square root of 196?",
                            "What is the square root of 225?",
                            "What is the square root of 256?",
                            "What is the square root of 289?",
                            "What is the square root of 324?",
                            "What is the square root of 361?",
                            "What is the square root of 400?",
                            "What is the square root of 1?",
                            };
string partans[20]={"2",
                        "3",
                        "4",
                        "5",
                        "6",
                        "7",
                        "8",
                        "9",
                        "10",
                        "11",
                        "12",
                        "13",
                        "14",
                        "15",
                        "16",
                        "17",
                        "18",
                        "19",
                        "20",
                        "1"};

提前感谢!

您可以创建一个索引向量并为其使用std::shuffle

您也可以使用半手动洗牌。例:

srand(time(NULL));
rand();
unsigned indexes[cnt];
unsigned fact = 0;
while(fact < cnt)
{
    const unsigned r = rand() % cnt;
    bool was = false;
    for(unsigned i = 0; i < fact; ++i)
    {
        if(indexes[i] == r) {
            was = true;
            break;
        }
    }
    if(!was)
    {
        indexes[fact] = r;
        ++fact;
    }
}
for(unsigned i = 0; i < cnt; ++i)
{
    const unsigned j = indexes[i];
    cout << "Q: " << questions[j] << "; A: " << answers[j] << endl;
}

正如在对问题的评论中提到的,您可以使用 cstdlib 中的 rand() 并限制返回的随机数,请使用模数 (%)。

srand(time(NULL));
index = rand() % array_size;

然后使用该索引访问数组中的问题。

cout << questionpart[index] << endl;

编辑:您可能需要使用ctime作为srand

EDIT2:要在不重复相同问题的情况下随机化,您可能需要存储已经用于跟踪它的问题,或者如果您不再需要它,只需将其从数组中完全删除。

对于更高级的方法,您可以定义一个将保存所有内容(问题、答案和状态)的对象像这样:

class Question {
    string question;
    string answer;
    bool wasAsked = false;
}

然后从中创建一个数组(或者最好是动态数组支持的矢量)

Question questions[array_size];