【问题标题】:How to create a random chance of word/sentence output in c++如何在 C++ 中创建单词/句子输出的随机机会
【发布时间】:2023-04-02 20:35:01
【问题描述】:

我只是想知道我怎么能做到这一点。这是我想要的代码的 sn-p:

/**< CASPOR's question script */
if (input == "N")
{
    cout << "Ok, " << name << " would you like to ask me a simple question?\n";
    cin >> input;
    {
        if (input == "Y")
        {
            while ("Y")
            {
                cout << "Ask away!\n";
                cin.ignore();
                getline(cin, input);
                {
                    if (input == "Who are you?")
                    {
                        cout << "I am CASPOR, or a C++ Automated Speech Program Of Recognition.\n";
                    }

                    if (input == "What is your purpose?")
                    {
                        cout << "My purpose is to entertain and amaze. Almost like a boredom breaker.\n";
                    }

                    if (input == "What can you do?")
                    {
                        cout << "I can do anything the developers program me to do!";
                    }
                    cout << "Would you like to ask another question?\n";
                    cin >> input;
                    {
                        if (input == "Y")
                            continue;
                    }
                }
            }
        }
    }
}

"CASPOR" 回答你的问题的地方,我怎么能包括他每次都会说不同的话的机会?

【问题讨论】:

  • 这个间距是怎么回事?使用任意缩进很难阅读您的代码。

标签: c++ random word code-snippets


【解决方案1】:

您可以将您的答案存储在一个容器中(通常是 std::vector&lt;std::string&gt;&gt;)并使用 std::shuffle 对其进行随机化。

然后只选择向量中的第一个答案。

示例:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <random>
#include <chrono>

int main()
{
    // Initialize the seed
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    // Store the answers
    std::vector<std::string> answers = { "Answer1" , "Answer2", "Answer3", "Answer4" };

    for(std::size_t n = 0 ; n <10 ; ++n)
    {
        // Randomize the vector
        std::shuffle(std::begin(answers), std::end(answers), std::default_random_engine(seed));
        std::cout << answers[0] << '\n';
    }
}

Live demo

注意事项:

  • 您的if 应该是if ... else if ... else if ...
  • 您的while ("Y") 毫无意义,只需while(true)

【讨论】:

    【解决方案2】:

    你会有类似的东西:

    std::string possibleAnswers[] = {"Answer 1", "Answer 2", "Answer 3", "etc."};
    

    然后可能会做类似的事情:

    std::cout << possibleAnswers[rand() % numPossibleAnswers] << "\n";
    

    这是基本的东西,但如果您想要更复杂的东西,您可以设置一个从 .csv 加载答案的系统,其中每一行都有不同问题的可能答案。

    【讨论】:

      【解决方案3】:

      这是一个在预定义字符串向量上使用&lt;random&gt; 的小例子:

      default_random_engine my_random;            // random generator  but not yet seeded -> so always the same numbers
      my_random.seed(time(NULL));                 // intiialisze with at least a little bit of randomness
      
      vector<string> sentences = { "Hello", "Hi !", "Happy to see you !", "etc..." };
      uniform_int_distribution<int> sentences_dist(0, sentences.size()-1);    // uniform distribution between 0 and size of vector (equal proba to have any of it)
      
      for (int i = 0; i < 15; i++)
          cout << sentences[sentences_dist(my_random)] << endl; 
      

      【讨论】:

        【解决方案4】:

        您可以创建一个问题数据库并编写一个函数,该函数在调用时会返回一个随机问题,如下例所示:

        #include <iostream>
        #include <random>
        #include <array>
        #include <string>
        
        std::array<std::string, 3> questions {"Who are you?", "What is your purpose?", "What can you do?"};
        
        std::string random_question() {
          std::random_device rd;
          std::mt19937 gen(rd());
          std::uniform_int_distribution<> dis(0, questions.size() - 1);
        
          return questions[dis(gen)];
        }
        
        int main() {
          for(int n = 0; n < 10; ++n) std::cout << random_question() << std::endl;
        }
        

        LIVE DEMO

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多