C++ 带循环和消去的随机生成概率对策

C++ 带循环和消去的随机生成概率对策,c++,loops,random,probability,C++,Loops,Random,Probability,这是我的任务: 在瓦尼亚的土地上,亚伦、鲍勃和查理争论他们中的哪一个 他是有史以来最伟大的解谜者。为了一劳永逸地结束争论,他们同意了 与死神决斗。Aaron的射门很差,只命中目标的概率很小 1/3. 鲍勃稍微好一点,命中目标的概率是1/2。查理是前男友- 精力充沛的射手,从未错过。命中意味着杀死,被击中的人退出决斗。 为了弥补射击技术上的不公平,三人决定 会依次出现,先是亚伦,然后是鲍勃,然后是查理。循环 会一直重复,直到有一个人站着。那个人将永远被人们记住 时间是有史以来最伟大的解谜者 一个

这是我的任务:


在瓦尼亚的土地上,亚伦、鲍勃和查理争论他们中的哪一个 他是有史以来最伟大的解谜者。为了一劳永逸地结束争论,他们同意了 与死神决斗。Aaron的射门很差,只命中目标的概率很小 1/3. 鲍勃稍微好一点,命中目标的概率是1/2。查理是前男友- 精力充沛的射手,从未错过。命中意味着杀死,被击中的人退出决斗。 为了弥补射击技术上的不公平,三人决定 会依次出现,先是亚伦,然后是鲍勃,然后是查理。循环 会一直重复,直到有一个人站着。那个人将永远被人们记住 时间是有史以来最伟大的解谜者

一个明显而合理的策略是让每个人都朝着最准确的射手射击 活着,理由是这个射手是最致命的,而且有最好的反击机会。 使用此策略编写一个程序来模拟决斗。你的程序应该使用random 问题中给出的数字和概率,用于确定射手是否击中目标 目标。您可能需要创建多个子例程和函数来完成 问题

暗示 假设Aaron击中目标的几率是1/3。您可以通过以下方式模拟此概率: 生成一个介于1和99之间(含99)的随机变量R。发生这种情况的概率是多少 R小于33?1/3。你知道这是怎么回事吗?如果是R33,那么我们可以 模拟亚伦击中目标。如果R>33,那么他就错过了。您还可以生成一个随机变量 变量介于0和1之间,并检查其是否小于0.33


以下是我在从头重写两次后最终能够编写的代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main ()
{
    int aaron=1, bob=1,charlie=1, a=0,b=0,c=0,counter=0;
    srand(time(0));
    a= rand() % 100;
    if (a<=33)//if aaron kills charlie
    {
        charlie=0;

        b= rand() % 100; //bob choots at aaron
        if (b<=50)  //if bob kills aaron
        {
            aaron=0;
            cout<<"error1"<<endl;
        }
        else if (b>50)
        {
            while (b>50) //if bob misses
            {
                a= rand() % 100; //aaron shoots at bob
                if (a<=33)// if he kills bob
                {
                    bob=0;
                    cout<<"error2"<<endl;
                }
                else //if he misses
                {
                    b= rand() % 100;//bob shoots at aaron

                    if (b<=50)//if he kills him
                    {
                        aaron=0;
                        cout<<"error3"<<endl;
                    }
                    else //if he misses then the loop continues
                    {
                        counter++;
                    }
                }
            }
        }
    }
    else if (a>33)//aaron misses
    {
        b= rand() % 100; //bob shoots at charlie
        if (b<=50) //he kills charlie
        {
            charlie = 0;
            a= rand() % 100; //aaron shoots at bob
            if (a<=33)//aaron kills bob
            {
                bob=0;
                cout<<"error4"<<endl;
            }
            else //if not
            {
                b= rand() % 100;//bob shoots at aaron
                if (b<=50) // if he kills aaron
                {
                    aaron=0;
                    cout<<"error5"<<endl;
                }
                else if (b>50)
                {
                    while (b>50)//if not then begin loop
                    {
                        a= rand() % 100; //aaron shoots at bob
                        if (a<=33) //if he kills him
                        {
                            bob=0;
                            cout<<"error6"<<endl;
                        }
                        else
                        {
                            b= rand() % 100;;   //if not bob shoots at aaron
                            if (b<=50)// if he kills aaron
                            {
                                aaron=0;
                                cout<<"error7"<<endl;
                            }
                            else
                            {
                                counter++; //if not loop around
                            }
                        }

                    }
                }
            }
        }

        else //he misses so charlies kills bob
        {
            bob=0;
            a= rand() % 100; //aaron shoots at charlie
            if (a<=33) //aaron kills charlie
            {
                charlie=0;
                cout<<"error8"<<endl;
            }
            else // or charlie kills aaron
            {
                aaron=0;
                cout<<"error9"<<endl;
            }
        }


        if (charlie==0 && bob==0)
        {
            cout<<"Aaron wins."<<endl;
        }
        else if (aaron==0 && bob==0)
        {
            cout<<"Charlie wins."<<endl;
        }
        else if (charlie==0 && aaron==0)
        {
            cout<<"Bob wins."<<endl;
        }
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int aaron=1,bob=1,charlie=1,a=0,b=0,c=0,计数器=0;
srand(时间(0));
a=rand()%100;

如果(aFirst point-
a=rand()%100;
不包含1..99,则包含0..99。您可以使用
a=(rand()%99)+1
。这仍然不是一个完全一致的分布,因为
rand
给出的随机数范围可能无法被99整除。可能的修复方法包括

  • 按照WhozCraig的建议使用C++11随机数库

  • 在循环中生成随机数。例如,如果
    rand()
    给出的值
    =9900
    ,请将其丢弃并重试。将此循环放入函数中,这样您就不必每次都担心它

  • 别担心,对于这样的事情,如果你应该关心这个问题,你可能已经被警告过了

  • 下一点是代码,例如

    if (b<=50)  //if bob kills aaron
    {
        aaron=0;
        cout<<"error1"<<endl;
    }
    else if (b>50)
    
    如果Aaron射杀Bob,
    b
    未被修改,因此循环会再次重复。Aaron可以在Bob最终未命中之前反复射杀Bob,Bob最终得到一个回合,即使他已经死了。您需要更改循环条件-可能在其他情况下也是如此,尽管我没有检查。我可能会使用
    完成
    标志

    接下来,对于
    a
    b
    (我甚至不认为你在使用
    c
    )你真的不需要单独的变量-对于
    random
    或类似的变量,只需使用单个变量
    r

    最后,我将使用的结构是

    while num_alive > 1
      if aaron_alive
        if charlie_alive
          aaron tries to shoot charlie
        else
          aaron tries to shoot bob
    
      if bob_alive
        if charlie_alive
          bob tries to shoot charlie
        else
          bob tries to shoot aaron
    
      if charlie_alive
        if bob_alive
          charlie tries to shoot bob
        else
          charlie tries to shoot aaron
    
    这里的要点是,如果您试图在控制流中记住太多关于谁活着谁死的信息,代码就会变得复杂。在下一次迭代中再次检查似乎是不必要的工作,但这些检查是微不足道的,即使不是,这也是过早的优化。您应该更喜欢简单的代码


    在本例中,您还可以查看这三个
    if _alive
    块的相似性,以及它们如何被分解成函数。

    旁注:如果您应该具有均匀分布,您可能希望选择使用均匀分布RNG算法。如果您可以使用C++11,那么已经提供了这样的分布你在
    中,也就是说。设置有点奇怪,但最终值得。正如你所知,
    rand()%100
    不会给你一个统一的分布。而且,
    rand()%100
    给出的值是0..99,而不是问题所要求的1..99,所以即使分布是统一的
    
    
    while num_alive > 1
      if aaron_alive
        if charlie_alive
          aaron tries to shoot charlie
        else
          aaron tries to shoot bob
    
      if bob_alive
        if charlie_alive
          bob tries to shoot charlie
        else
          bob tries to shoot aaron
    
      if charlie_alive
        if bob_alive
          charlie tries to shoot bob
        else
          charlie tries to shoot aaron