C ++掷硬币仿真器不工作 [英] C++ coin toss simulator not working

查看:221
本文介绍了C ++掷硬币仿真器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该创建一个抛硬币在C ++ SIM卡。我有这个code;但是,我得到的所有20次投掷的意思,我不是得到全部二十抛头或我得到全部二十抛尾相同的输出。我从来没有得到任何的变化,因为我应该。我感觉好像我已经正确地做了一切,除了一个组成部分。任何人都可以点我在正确的方向?

code:

 的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&; cstdlib>
#包括LT&;&的ctime GT;使用命名空间std;类硬币
{
上市:
硬币()
{
    折腾();
}无效折腾()
{
    无符号的种子=时间(0);
    函数srand(种子);    INT NUM =(RAND()%(2));    如果(NUM == 0)
    {
        sideUp =(头);
    }
    其他
    {
        sideUp =(尾巴);
    }
}串getSideUp()
{
    返回sideUp;
}
私人的:
串sideUp;
};诠释的main()
{
INT totHeads = 0,totTails = 0;抛硬币;COUT<<翻转#1:<< flip.getSideUp()<< ENDL;如果(flip.getSideUp()==(团长))
{
    totHeads + = 1;
}
其他
{
    totTails + = 1;
}
为(中间体X = 2; X&下; = 20; X ++)
{
    flip.toss();
    COUT<<翻转#<< X<<:<< flip.getSideUp()<< ENDL;    如果(flip.getSideUp()==(团长))
{
    totHeads + = 1;
}
其他
{
    totTails + = 1;
}
}COUT<<元首总量分别为:<< totHeads<< ENDL;
COUT&所述;&下;尾料的总量分别为:&所述;&所述; totTails&所述;&下; ENDL;返回0;
}


解决方案

正在播种随机数发生器每折腾,这是不是要走的路。在大多数情况下,你应该每个程序的种子PRNG一次。在你的情况,你会得到相同的种子,因为你是在很短时间内量播种多次,因此你得到相同的结果每次折腾后,作为PRNG开始重新使用相同的种子。只需删除行

 无符号的种子=时间(0);
函数srand(种子);

折腾函数,并把它们作为第一行主。事实上,你可以摆脱种子变量完全和只写

 函数srand(时间(0));

INT的main(){

如果你有机会获得C ++ 11,你应该使用的新功能和新的PRNG从<随机> ,而不是老Ç 风格兰特()/函数srand()组合。例如参见 HTTP://en.cp$p$pference.com/w/cpp /数字/随机的一些介绍。

I am supposed to create a coin toss sim in c++. I have this code; however, I am getting the same output for all 20 tosses meaning I either get heads for all twenty tosses or I get tails for all twenty tosses. I never get any variation as I am supposed to. I feel as if I have done everything correctly except for that one part. Can anyone point me in the right direction?

Code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

class Coin
{
public:
Coin()
{
    toss();
}

void toss()
{
    unsigned seed = time(0);
    srand(seed);

    int num = (rand()%(2));

    if (num == 0)
    {
        sideUp = ("Heads");
    }
    else
    {
        sideUp = ("Tails");
    }
}

string getSideUp()
{
    return sideUp;
}
private:
string sideUp;
};

int main()
{
int totHeads = 0, totTails = 0;

Coin flip;

cout<<"Flip #1: "<<flip.getSideUp()<<endl;

if (flip.getSideUp() == ("Heads"))
{
    totHeads += 1;
}
else
{
    totTails += 1;
}


for (int x = 2; x <= 20; x++)
{
    flip.toss();
    cout<<"Flip #"<<x<<": "<<flip.getSideUp()<<endl;

    if (flip.getSideUp() == ("Heads"))
{
    totHeads += 1;
}
else
{
    totTails += 1;
}
}

cout<<"The total amount of heads were: "<<totHeads<<endl;
cout<<"The total amount of tails were: "<<totTails<<endl;

return 0;
}

解决方案

You are seeding the random number generator every toss, which is not the way to go. Most of the time, you should seed your PRNG only once per program. In your case, you get the same seed since you are seeding multiple times in a very short amount of time, hence you get the same result after each toss, as the PRNG starts reuses the same seed. Simply remove the lines

unsigned seed = time(0);
srand(seed);

from your toss function and put them as the first lines in main. In fact, you can get rid of the seed variable entirely and just write

srand(time(0));

after int main(){

If you have access to C++11, you should use the new functions and PRNGs from <random>, instead of old C-style rand()/srand() combinations. See e.g. http://en.cppreference.com/w/cpp/numeric/random for some introduction.

这篇关于C ++掷硬币仿真器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆