C++ 街头骰子模拟-看起来像是代码偏差?
我有一个简单的基于控制台的骰子游戏(街头掷骰子),在这一点上,它基本上只是一个模拟 但每次我运行代码时,玩家似乎都有优势。 随着时间的推移,情况只会变得更糟 这是我的密码C++ 街头骰子模拟-看起来像是代码偏差?,c++,dice,C++,Dice,我有一个简单的基于控制台的骰子游戏(街头掷骰子),在这一点上,它基本上只是一个模拟 但每次我运行代码时,玩家似乎都有优势。 随着时间的推移,情况只会变得更糟 这是我的密码 #include <iostream> #include <ctime> #include <Windows.h> int Wins = 0, Losses = 0; using namespace std; class Dice { private: int NumSides
#include <iostream>
#include <ctime>
#include <Windows.h>
int Wins = 0, Losses = 0;
using namespace std;
class Dice
{
private:
int NumSides;
public:
Dice(int NumSides = 6)
{
this->NumSides = NumSides;
}
~Dice()
{
}
int Roll(int NumDice)
{
int Result = 0;
for (int i = 0; i < NumDice; i++)
Result += (rand() % NumSides) + 1;
return Result;
}
};
void PrntScore()
{
cout << "Wins = " << Wins << " Losses = " << Losses << endl;
}
int Won()
{
cout << "YOU WIN!" << endl;
Wins++;
PrntScore();
return 0;
}
int Lost()
{
cout << "YOU LOSE!" << endl;
Losses++;
PrntScore();
return 0;
}
int main()
{
srand(time(NULL));
Dice D;
while (1)
{
int FirstRoll = D.Roll(2);
cout << "First Roll = " << FirstRoll << endl;
if (FirstRoll == 7 || FirstRoll == 11) { Won(); }
else if (FirstRoll == 2 || FirstRoll == 3 || FirstRoll == 12) { Lost(); }
else
{
cout << "Point set = " << FirstRoll << endl;
int NewRoll = 0;
do
{
NewRoll = D.Roll(2);
cout << "New Roll = " << NewRoll << endl;
Sleep(500);
} while (NewRoll != FirstRoll && NewRoll != 7);
if (NewRoll == FirstRoll)
Won();
else
Lost();
}
Sleep(500);
}
return 0;
}
#包括
#包括
#包括
积分赢=0,损失=0;
使用名称空间std;
类骰子
{
私人:
努米塞德斯;
公众:
骰子(整数NumSides=6)
{
此->NumSides=NumSides;
}
~Dice()
{
}
整数滚动(整数)
{
int结果=0;
对于(int i=0;i coutrand、srand和rand\u MAX有很多问题,不建议使用它们。应该考虑使用C++11
经过100次迭代后,我看到的损失比成功的多。你可以尝试使用更好的rand()
实现,比如SFMT。你的编译器内置的rand()
很可能是垃圾。你得到了什么赢和输的数字?使用::std::uniform_int_distribution
:std::mt19937
。不仅仅是rand()
糟糕,使用模也是不一致的。