掷 2 个骰子 1000 次
Roll 2 dice 1000 times
我的教授要求我们编写一个程序:
用一个循环来模拟一对骰子的滚动一千次(这里我觉得for循环会很有用)。
每次迭代,循环需要计算每个值从 2 到 12 的次数(这里我认为 if/else 语句适用)
当循环结束时,必须显示每个值(从2到12)出现的次数。
他的作业结构如下:
他希望我们使用一个进入 1000 次 for 循环的函数,每次函数调用调用另一个函数两次(以模拟掷出两个骰子)。
让我解释一下我设法放下的东西
//
// main.cpp
// RollingDice
#include <iostream>
#include <ctime>
using namespace std;
int roll();
int rollDice();
int main(int argc, const char * argv[])
{
for (int i = 1; i < 1000; i++)
{
rollDice(); //This is the function that is supposed to call the roll();
//function two times. This makes sense to me that TWO DICE
//are being rolled 1000 times.
}
int result; //These two statements was where I was just hoping for something
//to work. I put these variable statements outside of the for
//loop because I was thinking that the int rollDice(); function
//definition(below) wouldn't know how to use it otherwise. I
//know that doesn't make sense, but I just can't explain why.
result = rollDice();
}
int roll()
{ //This function was provided to us by my professor.
static bool randomInitialized = false;
int points;
if (!randomInitialized)
{
srand((unsigned int) time(NULL));
randomInitialized = true;
}
points = (rand() % 6) + 1;
return points;
}
int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice.
//The return statement in this function was my attempt of returning the sum
//of the values of the two dice.
roll();
roll();
return result;
}
除了程序的这一部分不工作之外,我还有另一个问题是确定一种方法来为出现的每个值设置一个计数器(但是,我想程序的那部分属于 for 循环. 这就是我所知道的。)。从昨天开始,我就对这个节目进行了深入的思考。我今天又回来了,希望有一个新的想法能解决它,但我仍在挣扎。非常感谢任何帮助。
你可以做这样的事情(需要一些改动以适应你教授的其余代码)
int rollDice()
int main(int argc, const char * argv[])
{
srand(time(0));// randomly seed every time you run the code
int dice1;
int dice2;
int storageData[11];//stores the number of times the values from 2 to 12 appears;
for(int i=0; i<1000; i++)
{
dice1=rollDice();
dice2=rollDice();
int sum=dice1+dice2;
storageData[sum-2]+=1; // updates the number of times the sum has appeared.
}
cout << "the value x has appeared "<< storageData[x-2] <<" times"<< endl; // change the value of x depending on which sum number you want.
}
int rollDice()
{
int x=rand()%6+1;// rand()%6 produces a no. from 0-5. rand()%6+1 produces a number from 1-6.
return x;
}
注意,在上面的代码中,我们为每个元素减去 (-2),因为总和从 2 而不是 0 开始。
1-用地图数2到12每个数的次数:(最实用)
int sumOfDice;
map <int,int> diceOccurances;
for (int i=0; i < 1000; i++)
{
sumOfDice=rollDice();
diceOccurances[sumOfDice];
// Here you are storing how many times each of the dice values occured. Here's
// how you access the map;
}
for (auto const& x : socks)
{
cout <<" Dice Total Number: " << x.first ;
cout <<" Dice Number of Occurances: "<< x.second<<endl;
}
int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice.
//The return statement in this function was my attempt of returning the sum
//of the values of the two dice.
int die1,die2;
die1= roll();
die2= roll();
result = die1+die2;
return result;
}
2- 使用 if/else(或切换);
int two, three, four, five, six, seven, eight, nine, ten ,eleven ,twelve;
two=three=four=five=six=seven=eight=nine=ten=eleven=twelve=0;
for (int i=0; i < 1000; i++)
{
if ( rollDice()==2) two++;
if (rollDice()==3) three++;
if (rollDice()==4) four++;
// and so forth until twelve++;
}
int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice.
//The return statement in this function was my attempt of returning the sum
//of the values of the two dice.
int die1,die2;
die1= roll();
die2= roll();
result = die1+die2;
return result;
}
表达式 roll()
的计算结果为一个数字。要添加数字,我们使用 +
。要 return 一个值,我们使用 return
.
将它们放在一起,我们得到一个简单的函数来计算两个卷的总和
int rollDice() { return roll() + roll(); }
如果您有一个编号序列,并且数字靠得很近并且从 0 附近开始,则标准库的 SequenceContainer 之一是整个序列的合适持有者。
这里的东西是特定投掷的计数。我们预先确切知道可用值(包括 2 - 12),因此 std::array
是合适的。任何至少可以容纳 1000 的整数值都适用于计数。我这里选择std::size_t
#include <array>
#include <iostream>
int main()
{
std::array<std::size_t, 13> counts {};
这会给我们 13 个 0,从位置 0 开始
for (std::size_t i = 0; i < 1000; ++i)
{
++counts[rollDice()];
我们用rollDice选择哪个数字,然后用它来select一个计数来增加
}
for (std::size_t i = 2; i < 13; ++i)
{
我们现在可以循环我们的结果,显示计数
std::cout << "The count for " << i << " is " << counts[i] << std::endl;
}
}
我的教授要求我们编写一个程序:
用一个循环来模拟一对骰子的滚动一千次(这里我觉得for循环会很有用)。
每次迭代,循环需要计算每个值从 2 到 12 的次数(这里我认为 if/else 语句适用)
当循环结束时,必须显示每个值(从2到12)出现的次数。
他的作业结构如下:
他希望我们使用一个进入 1000 次 for 循环的函数,每次函数调用调用另一个函数两次(以模拟掷出两个骰子)。
让我解释一下我设法放下的东西
//
// main.cpp
// RollingDice
#include <iostream>
#include <ctime>
using namespace std;
int roll();
int rollDice();
int main(int argc, const char * argv[])
{
for (int i = 1; i < 1000; i++)
{
rollDice(); //This is the function that is supposed to call the roll();
//function two times. This makes sense to me that TWO DICE
//are being rolled 1000 times.
}
int result; //These two statements was where I was just hoping for something
//to work. I put these variable statements outside of the for
//loop because I was thinking that the int rollDice(); function
//definition(below) wouldn't know how to use it otherwise. I
//know that doesn't make sense, but I just can't explain why.
result = rollDice();
}
int roll()
{ //This function was provided to us by my professor.
static bool randomInitialized = false;
int points;
if (!randomInitialized)
{
srand((unsigned int) time(NULL));
randomInitialized = true;
}
points = (rand() % 6) + 1;
return points;
}
int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice.
//The return statement in this function was my attempt of returning the sum
//of the values of the two dice.
roll();
roll();
return result;
}
除了程序的这一部分不工作之外,我还有另一个问题是确定一种方法来为出现的每个值设置一个计数器(但是,我想程序的那部分属于 for 循环. 这就是我所知道的。)。从昨天开始,我就对这个节目进行了深入的思考。我今天又回来了,希望有一个新的想法能解决它,但我仍在挣扎。非常感谢任何帮助。
你可以做这样的事情(需要一些改动以适应你教授的其余代码)
int rollDice()
int main(int argc, const char * argv[])
{
srand(time(0));// randomly seed every time you run the code
int dice1;
int dice2;
int storageData[11];//stores the number of times the values from 2 to 12 appears;
for(int i=0; i<1000; i++)
{
dice1=rollDice();
dice2=rollDice();
int sum=dice1+dice2;
storageData[sum-2]+=1; // updates the number of times the sum has appeared.
}
cout << "the value x has appeared "<< storageData[x-2] <<" times"<< endl; // change the value of x depending on which sum number you want.
}
int rollDice()
{
int x=rand()%6+1;// rand()%6 produces a no. from 0-5. rand()%6+1 produces a number from 1-6.
return x;
}
注意,在上面的代码中,我们为每个元素减去 (-2),因为总和从 2 而不是 0 开始。
1-用地图数2到12每个数的次数:(最实用)
int sumOfDice;
map <int,int> diceOccurances;
for (int i=0; i < 1000; i++)
{
sumOfDice=rollDice();
diceOccurances[sumOfDice];
// Here you are storing how many times each of the dice values occured. Here's
// how you access the map;
}
for (auto const& x : socks)
{
cout <<" Dice Total Number: " << x.first ;
cout <<" Dice Number of Occurances: "<< x.second<<endl;
}
int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice.
//The return statement in this function was my attempt of returning the sum
//of the values of the two dice.
int die1,die2;
die1= roll();
die2= roll();
result = die1+die2;
return result;
}
2- 使用 if/else(或切换);
int two, three, four, five, six, seven, eight, nine, ten ,eleven ,twelve;
two=three=four=five=six=seven=eight=nine=ten=eleven=twelve=0;
for (int i=0; i < 1000; i++)
{
if ( rollDice()==2) two++;
if (rollDice()==3) three++;
if (rollDice()==4) four++;
// and so forth until twelve++;
}
int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice.
//The return statement in this function was my attempt of returning the sum
//of the values of the two dice.
int die1,die2;
die1= roll();
die2= roll();
result = die1+die2;
return result;
}
表达式 roll()
的计算结果为一个数字。要添加数字,我们使用 +
。要 return 一个值,我们使用 return
.
将它们放在一起,我们得到一个简单的函数来计算两个卷的总和
int rollDice() { return roll() + roll(); }
如果您有一个编号序列,并且数字靠得很近并且从 0 附近开始,则标准库的 SequenceContainer 之一是整个序列的合适持有者。
这里的东西是特定投掷的计数。我们预先确切知道可用值(包括 2 - 12),因此 std::array
是合适的。任何至少可以容纳 1000 的整数值都适用于计数。我这里选择std::size_t
#include <array>
#include <iostream>
int main()
{
std::array<std::size_t, 13> counts {};
这会给我们 13 个 0,从位置 0 开始
for (std::size_t i = 0; i < 1000; ++i)
{
++counts[rollDice()];
我们用rollDice选择哪个数字,然后用它来select一个计数来增加
}
for (std::size_t i = 2; i < 13; ++i)
{
我们现在可以循环我们的结果,显示计数
std::cout << "The count for " << i << " is " << counts[i] << std::endl;
}
}