C++ 骰子计数器工作不正常(初学者)
这是为了模拟投掷2个6面骰子。但当我投10次球时,它会随机投任意多的球(4、5、6等)。我错过什么了吗C++ 骰子计数器工作不正常(初学者),c++,counter,C++,Counter,这是为了模拟投掷2个6面骰子。但当我投10次球时,它会随机投任意多的球(4、5、6等)。我错过什么了吗 #include <iostream> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int throwDice() // returns random number ranged 2-12 { int
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice() // returns random number ranged 2-12
{
int x = (rand() % 11) + 2;
return x;
}
int main()
{
srand (time(NULL));
int y;
cout << "Roll dice how many times?" << endl;
cin >> y;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int a9 = 0;
int a10 = 0;
int a11 = 0;
int a12 = 0;
for (int i = 0; i < y; i++)
{
throwDice();
if (throwDice() == 2)
a2++;
else if (throwDice() == 3)
a3++;
else if (throwDice() == 4)
a4++;
else if (throwDice() == 5)
a5++;
else if (throwDice() == 6)
a6++;
else if (throwDice() == 7)
a7++;
else if (throwDice() == 8)
a8++;
else if (throwDice() == 9)
a9++;
else if (throwDice() == 10)
a10++;
else if (throwDice() == 11)
a11++;
else if (throwDice() == 12)
a12++;
}
cout << "2 = " << a2 << endl;
cout << "3 = " << a3 << endl;
cout << "4 = " << a4 << endl;
cout << "5 = " << a5 << endl;
cout << "6 = " << a6 << endl;
cout << "7 = " << a7 << endl;
cout << "8 = " << a8 << endl;
cout << "9 = " << a9 << endl;
cout << "10 = " << a10 << endl;
cout << "11 = " << a11 << endl;
cout << "12 = " << a12 << endl;
system("pause");
}
#包括
#包括
#包括
#包括
使用名称空间std;
int throwDice()//返回范围为2-12的随机数
{
int x=(rand()%11)+2;
返回x;
}
int main()
{
srand(时间(空));
int-y;
库蒂;
int a2=0;
int a3=0;
int a4=0;
int a5=0;
int a6=0;
int a7=0;
int a8=0;
int a9=0;
int a10=0;
int a11=0;
int a12=0;
for(int i=0;i
您正在调用throwDice()
一次以生成抛出(正确),然后在if
语句(不正确)中的每个计算条件中再次调用。将抛出的结果保存在变量中,并在检查中使用该变量
您的throwDice
函数不会模拟投掷两个6面骰子,但会模拟投掷一个11面骰子(上面印有数字2-11)。这会产生不同。(这不是一个编程问题,而是一个数学问题。一旦您理解了其背后的数学,就很容易更正您的函数。)
- 您正在调用
throwDice()
一次以生成抛出(正确),然后在if
语句(不正确)中的每个计算条件中再次调用。将抛出的结果保存在变量中,并在检查中使用该变量
- 您的
throwDice
函数不会模拟投掷两个6面骰子,但会模拟投掷一个11面骰子(上面印有数字2-11)。这会产生不同。(这不是一个编程问题,而是一个数学问题。一旦您理解了其背后的数学,就很容易更正您的函数。)
您的循环应该如下所示
int result;
for (int i = 0; i < y; i++)
{
result = throwDice();
if (result == 2)
a2++;
else if (result == 3)
a3++;
else if (result == 4)
a4++;
else if (result == 5)
a5++;
else if (result == 6)
a6++;
else if (result == 7)
a7++;
else if (result == 8)
a8++;
else if (result == 9)
a9++;
else if (result == 10)
a10++;
else if (result == 11)
a11++;
else if (result == 12)
a12++;
}
int结果;
for(int i=0;i
还有,throwDice()
函数并不等同于掷2个骰子。您创建了一个函数,该函数将有均等的机会将所有值从2掷到12。例如,当您掷2个骰子时,掷6的可能性远远大于掷12的可能性。您应该创建两个介于1和6之间的数字,并将它们相加以获得throwDice()的返回值
函数。您的循环应该如下所示
int result;
for (int i = 0; i < y; i++)
{
result = throwDice();
if (result == 2)
a2++;
else if (result == 3)
a3++;
else if (result == 4)
a4++;
else if (result == 5)
a5++;
else if (result == 6)
a6++;
else if (result == 7)
a7++;
else if (result == 8)
a8++;
else if (result == 9)
a9++;
else if (result == 10)
a10++;
else if (result == 11)
a11++;
else if (result == 12)
a12++;
}
int结果;
for(int i=0;i
还有,throwDice()
函数并不等同于掷2个骰子。您创建了一个函数,该函数将有均等的机会将所有值从2掷到12。例如,当您掷2个骰子时,掷6的可能性远远大于掷12的可能性。您应该创建两个介于1和6之间的数字,并将它们相加以获得throwDice()的返回值
函数。我将编写以下代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice() // returns random number ranged 2-12
{
int x = (rand() % 11) + 2;
return x;
}
int main()
{
srand (time(NULL));
int y;
cout << "Roll dice how many times?" << endl;
cin >> y;
int total[13];
for( int i = 2; i <= 12; i++ )
total[i] = 0;
for (int i = 0; i < y; i++)
{
total[throwDice()]++;
}
for (int i = 2; i <= 12; i++)
cout << i << " = " << total[i] << endl;
system("pause");
}
#包括
#包括
#包括
#包括
使用名称空间std;
int throwDice()//返回范围为2-12的随机数
{
int x=(rand()%11)+2;
返回x;
}
int main()
{
srand(时间(空));
int-y;
库蒂;
整数总计[13];
对于(int i=2;i我将编写以下代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice() // returns random number ranged 2-12
{
int x = (rand() % 11) + 2;
return x;
}
int main()
{
srand (time(NULL));
int y;
cout << "Roll dice how many times?" << endl;
cin >> y;
int total[13];
for( int i = 2; i <= 12; i++ )
total[i] = 0;
for (int i = 0; i < y; i++)
{
total[throwDice()]++;
}
for (int i = 2; i <= 12; i++)
cout << i << " = " << total[i] << endl;
system("pause");
}
#包括
#包括
#包括
#包括
使用名称空间std;
int throwDice()//返回范围为2-12的随机数
{
int x=(rand()%11)+2;
返回x;
}
int main()
{
srand(时间(空));
int-y;
库蒂;
整数总计[13];
对于(int i=2;i我知道这并不能回答问题,但我不禁想知道,如果C++11对您可用,您是否会从中受益:
#include <random>
std::minstd_rand prng;
std::uniform_int_distribution<int> dice (1, 6);
int throwDice ()
{
return (dice(prng) + dice(prng));
}
int main ()
{
prng.seed(time(NULL));
// (code)
}
#包括
标准:minstd_rand prng;
标准:均匀分布骰子(1,6);
int throwDice()
{
返回(骰子(prng)+骰子(prng));
}
int main()
{
prng.seed(时间(空));
//(代码)
}
我知道这并不能回答问题,但我不禁想知道,如果C++11对您可用,您是否会从中受益:
#include <random>
std::minstd_rand prng;
std::uniform_int_distribution<int> dice (1, 6);
int throwDice ()
{
return (dice(prng) + dice(prng));
}
int main ()
{
prng.seed(time(NULL));
// (code)
}
#包括
标准:minstd_rand prng;
标准:均匀分布骰子(1,6);
int throwDice()
{
返回(骰子(prng)+骰子(prng));
}
int main()
{
prng.seed(时间(空));
//(代码)
}
当我打字时,似乎有人在这两个点上都打败了我。当我打字时,似乎有人在这两个点上都打败了我。不需要循环来初始化total
:int-total[13]={}
不同意你的最后一句话:这几个字节是为了一个非常崇高的目的而牺牲的,当然不是浪费。如果你在这个特定的例子中使用了索引技巧,你将浪费代码的清晰度