随机数生成器为一个或两个骰子
random number generator for one or two dice
这个头文件创建了一个大小取决于骰子数量的矢量。它包含投掷次数(rollsN),骰子数量(numDice)和骰子的面数(numSides—固定为6)。我认为问题出在第一个for循环中。当设置为一个骰子时,它将按预期运行,但当应用两个骰子时,它将以超出范围错误终止。
void randomNum(const int rollsN, int numDice, int numSides)
{
//Vector will hold an extra value (starts at 0, not 1).
vector<int> numVect((numDice*numSides) + 1);
//Starts a randomizer based on time
srand(time(0));
//provides random values for every possible result of the die
for(int i = 0; i < rollsN; i++)
{
int temp = 0; //holds the side of the dice that is chosen... or the sum of the two dice that are rolled
for(int j = 0; j < numDice; j++)
{
temp += (rand() % (numDice*numSides) + 1);
}
numVect.at(temp) += 1;
}
//prints how many times the die landed on that value
cout << endl << "RANDOMIZED RESULTS " << endl;
for(int i = 1; i <= (numDice*numSides); i++)
{
cout << i << " ----- " << numVect[i] << endl;
}
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl << "Histogram" << endl;
}
此代码
for(int j = 0; j < numDice; j++)
{
temp += (rand() % (numDice*numSides) + 1);
}
每个随机数从1到numDice*numSides
。您正在将这个numDice
添加几次,导致numDice*numDice*numSides
的潜在最大数量超出了您的范围。
改为:
for(int j = 0; j < numDice; j++)
{
temp += rand() % numSides + 1;
}
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 如何在C++中从两个单独的for循环中添加两个数组
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- C++多个实例的随机数生成器组种子中增强
- 为什么这4个不同的随机数生成器函数会产生相同的一系列数字
- 如何在我存储的两个变量之间生成随机数
- 如何在两个不同的数组中生成随机数?(C++)
- 使用 WELL512 的两个整数之间的 C++ 随机数
- 如何在C++生成器中运行两个窗体
- 两个区间[a,b]之间的二重随机数生成
- 伪随机数生成器给出相同的第一个输出,但随后的行为和预期的一样
- 在多个函数中使用相同的随机数生成器
- 随机数生成器为一个或两个骰子
- 生成两个(或多个)均匀分布的随机数
- Mersenne Twister随机生成器-两个VS2010并行运行
- 两个范围之间的随机数
- 如何创建两个独立的对象,显示随机数10次
- 我应该使用多少个随机数生成器
- 获取两个区间之间的随机数
- 我可以像在 R 中一样在 R 中拥有多个独立的随机数生成器吗C++