【问题标题】:How To Randomly Pick A Variable Using Rand()如何使用 Rand() 随机选择一个变量
【发布时间】:2013-12-17 23:04:38
【问题描述】:

我正在尝试用 C++ 制作一个基于文本的战斗机,这是我做的第一件事。到目前为止,我有这个:

//Text Based Fighter

#include <iostream>
#include <stdlib.h> //srand, rand
#include <string>

using namespace std;

int main() {

    //Player
    int playerHealth = 100;
    int attack1;
    int attack2;
    int attack3;
    string attack;
    int npc1;
    int npc2;

    cout << "Do you want to attack " << rand()[npc1,npc2];

    //varname = rand() % 10 + 1;

return 0;
}

我想要它做的是在 npc1 和 npc2 之间随机选择,谢谢。

还有任何关于我如何编写代码的 cmet 将不胜感激,我几天前才开始谢谢你,如果你需要更多细节,请随时询问,谢谢。

【问题讨论】:

    标签: c++ random


    【解决方案1】:

    您可以使用任意数量的变量组成的数组进行选择:

    int attack[n];  //For some int-constant n
    
    attack[rand() % n];  //choose a random attack-variable, use it
    

    【讨论】:

    【解决方案2】:

    对于只有 2 个选择,您可以使用三元表达式从 2 中取余数:

    int choice = rand() % 2 == 0 ? npc1 : npc2;
    

    如果您有两个以上的选择,或者即使您没有,您也可以用这些选择一个数组并对其进行索引。

    int npc_choices[2];
    int choice = npc_choices[rand() % 2];
    

    如果选择的数量不是 2 的幂,您可能会使用模 % 运算符在选择中引入非常小的偏差。如果您没有从事任何具有统计意义或有大量选择的工作,我不会担心。

    【讨论】:

      【解决方案3】:

      生成伪随机数时很容易出错。例如,在某些情况下,使用rand() % RANGE 可能会导致数字的细微分布错误。 (有关问题的示例,请参阅this reference。)

      如果您所做的事情微不足道,这可能无关紧要。

      如果你想要高质量的伪随机数,有办法修复rand()(参见上面的参考资料),但现代 C++ 也提供了&lt;random&gt;uniform_int_distribution

      这是一个模拟投掷 6 面骰子的示例,改编自 BoostC++ Reference 中的示例:

      #include <iostream>
      #include <random>
      
      std::random_device rd;
      std::mt19937 gen(rd());
      
      int roll_die() {
          std::uniform_int_distribution<> dist(1, 6);
          return dist(gen);
      }
      
      int main() {
          std::cout << roll_die() << std::endl;
      }
      

      表示dist(1, 6) 的部分可以更改为dist(0, 1),以在[0, 1](含)范围内产生均匀分布的输出。

      【讨论】:

        【解决方案4】:

        如果您在 C++11 中只有两个选择,您可以使用 std::bernoulli_distribution,这是一个过于简化的示例:

        #include <iostream>
        #include <random>
        
        int main()
        {
            std::random_device rd;
            std::mt19937 gen(rd());
            // give "true"1/2 of the time
            // give "false" 1/2 of the time
            std::bernoulli_distribution d(0.5);
        
            int npcs[2] = {100, 101};
        
            int index = d(gen) ? 0 : 1;
        
            std::cout << "Do you want to attack " << npcs[index] ;
        }
        

        使用数组更灵活,因为它很容易扩展到两个以上的选择,然后您需要使用std::uniform_int_distribution[0,N] 之间进行选择。

        从长远来看,使用rand() is not a good idea,虽然在许多简单的情况下它可能工作得很好。正如 Pete 所提到的,只要您了解 rand() 的限制,您就可以使用它,C FAQ 有一个很好的部分,How can I get random integers in a certain range?

        【讨论】:

        • 天哪,rand 已经存在了 40 年,直到现在没人知道它只在简单的情况下有用。一直以来没有人试图解决任何难题,这是一件好事。
        • @PeteBecker 不太确定您的观点是什么,但该演示文稿是我所知道的问题的最佳总结,大多数刚开始的程序员不会知道这一点。你知道一个更好的,我错过了什么吗?
        • 程序员喜欢排中的谬误:如果它不完美,那就没用了。尽管有 C++11s random number facilities can be easier to use, but that doesn't make rand` 无用的、带有倾向性标题的演讲。
        • @PeteBecker 当然,我认为我并没有试图如此极端,但在这一点上,随机标头是我们现在使用 C++ 的方式。如果可以的话,是否有充分的理由不使用它,除了用于教育目的以了解之前的内容及其局限性吗?
        • 是的,有充分的理由不使用它。例如,没有它。或者不理解它是如何工作的,当有人说“不要使用rand,改用这段代码”时,SO 上经常出现这种情况,而他们编写的代码具有与rand 完全相同的问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多