【问题标题】:Cannot get the random string无法获取随机字符串
【发布时间】:2014-04-02 16:46:11
【问题描述】:

我正在使用 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;
        }
    }
};

我使用的代码是这样的:

cout << English().get_questions(chapter);

虽然我有更多的行,但它们只是简单的coutcin 来获取章节和主题值。他们不会为此烦恼。

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

人们能否在这个千年里提高自己并去掉这个标签?

每当我更改逻辑时,我都会得到一个新结果,但在每种情况下都相似(该特定逻辑的代码执行)!我想要的是在哪里得到一个随机问题,每次执行代码时,我应该更改生成这个随机数的位置吗?还是我在其他地方做错了?

【问题讨论】:

  • 一些 cmets:C 随机数设施非常差:“标准”种子是一个时间值 (time(NULL)),只有 1 秒的精度,并且不返回与srand() 期望相同的类型(因此它失去了精度和可变性)。后来,rand() 不能保证产生均匀分布(不),但即使在这种情况下,你使用模运算符打破这种均匀性
  • 所以(我知道情况并非如此,您所公开的内容似乎是一个示例练习)如果您的随机数(您的 PRNG)的质量是一个问题,请考虑 不要使用 C 库,搜索质量好的 PRNG C++ 库。另外,请注意,自 C++11 以来,标准库附带了 random library,它比 C 等效项好 faaaaaaaaaaaaaar。

标签: c++ arrays random


【解决方案1】:

您应该使用srand 函数初始化随机数生成器,使用随机种子值来更改rand() 函数的这种行为。 您可以使用 srand (time(NULL)); 之类的东西来使用不同的种子初始化随机生成器。

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

【讨论】:

  • 他应该如何获得“随机种子值”——使用 rand()? :)
  • @FerdinandBeyer 我已经用详细信息和链接更新了我的答案
  • 非常感谢兄弟! :) 我从来不知道这个播种的东西,它现在起作用了! :)
  • @AfzaalAhmadZeeshan 欢迎兄弟 :)
【解决方案2】:

您没有播种随机数生成器,因此每次运行程序时都会得到相同的随机数序列。在程序开始时使用一次srand

【讨论】:

  • 那么,如果我在我的程序开始时使用rand(),或者我创建另一个方法,那会起作用吗?
【解决方案3】:

您需要使用在标题&lt;cstdlib&gt; 中声明的函数std::srand 来设置随机序列。

例如

class English {
public: 
    English() { if ( !init ) std::srand( unsigned( std::time( 0 ) ) ); init = true; }

    string get_questions (int number) const {
        if (number == 1) {
            // Chapter 1
            string questions[10] = { /*...*/ };
            string result = questions[rand() % 10] + "\n";
            return result;
        }
    }
private:
    static bool init;
};

bool English::init = false;

考虑到我对函数get_questions进行了更改

【讨论】:

    【解决方案4】:

    如果您使用的是兼容 C++11 的编译器,更好的解决方案是使用 &lt;random&gt; 库:

    // initialize your string array
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(0,9);
    int index = distribution(generator);
    return questions[index];
    

    【讨论】:

      猜你喜欢
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      相关资源
      最近更新 更多