无法获取随机字符串

Afz*_*han 1 c++ arrays random

我正在使用C++为随机问题创建一个应用程序.但我认为这不起作用(由于我的逻辑不好).我在想的是这个:

class English {
public: 
    string get_questions (int number) {
        if (number == 1) {
            // Chapter 1
            string questions[10] = {
                "In what way is man considere to be a lower species when compared to animals, in general?",
                "What specific triats of character make man the lowest animal in Mark Twain's views?",
                "What aspects of human nature are pointed when man is  compared with the anaconda, bees, roosters, cats.",
                "What specific traits of character make man the lowest  animal in Mark Twain's views?",
                "Discuss the Importance of the experiments conducted by the Mark Twain.",
                "Can people improve themselves and remove this label in thismillennium?",
                "What are the traits due to which man cannot claim to have reached the meanest of the Higher Animals?",
                "\"The damned Human Race\" was written in 1900, is it valid today?",
                "Do you think Mark Twain was accurate while comparing Human nature to that of the birds, insects and other animals?",
                "Why did Mark Twain rejected Darwin's theory, what were his conclusions in this regard?"
            };
            string result = questions[rand() % 9 + 0] + "\n";
            return result;
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码是这样的:

cout << English().get_questions(chapter);
Run Code Online (Sandbox Code Playgroud)

虽然我有更多的线条,但它们只是简单cout而且cin可以获得章节和主题值.他们不会为此烦恼.

这里的主要问题是每次编写代码时,编译执行时,每次都会提供相同的问题.例如,对于当前的随机逻辑,我得到了这个问题:

在这个千年里,人们可以改善自己并取消这个标签吗?

每当我改变逻辑时,我得到一个新结果,但在每个条件下都是相似的(该特定逻辑的代码执行)!我想要的是获取随机问题,每次执行代码时,我应该更改生成此随机数的位置吗?或者我在其他地方做错了什么?

Kar*_*ram 5

您应该使用srand函数使用随机种子值初始化随机数生成器来更改此rand()函数行为.您可以使用类似的srand (time(NULL));方法使用不同的种子初始化随机生成器.

请查看http://www.cplusplus.com/reference/cstdlib/srand/