for循环中的随机数生成器每次都给出相同的数字
Random number generator in a for loop gives same numbers each time
这个程序应该是一个非常原始的老虎机,有三个不同的"轮子"可以旋转。每个轮子都包含一定数量的字符。函数生成一个随机数,将其指定为每个车轮中的阵列位置,然后生成与该位置对应的符号。
我遇到的问题是,随机生成的数字在for循环的每次迭代中都不会改变。所以我基本上每次循环都会得到"X-X"或"X@-"。我搜索了以前提出的问题,找到了几个相关的问题,但似乎没有一个能解决我的特定问题。
为长代码道歉:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
const int WHEEL_POSITIONS = 30;
const char wheelSymbols[WHEEL_POSITIONS + 1] = "-X-X-X-X-X=X=X=X*X*X*X*X@X@X7X";
struct slotMachine
{
char symbols[WHEEL_POSITIONS + 1];
int spinPos;
char spinSymbol;
} wheels[3];
void startWheels(slotMachine []);
void spinWheels(slotMachine []);
void displayResults(slotMachine []);
bool getWinner(slotMachine []);
int main(void)
{
int spinNum;
cout << "How many times do you want to spin the wheel? ";
cin >> spinNum;
// Calls startWheels function
startWheels(wheels);
for (int i = 0; i < spinNum; i++)
{
// Calls spinWheels function
spinWheels(wheels);
// Calls displayResults function
displayResults(wheels);
// Calls function getWinner; if getWinner is true, outputs winning message
if (getWinner(wheels) == true)
{
cout << "Winner! Matched 3 of " << wheels[0].spinSymbol << "." << endl;
}
}
return 0;
}
// Function to initialize each wheel to the characters stored in wheelSymbols[]
void startWheels(slotMachine fwheels[3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < (WHEEL_POSITIONS + 1); j++)
{
fwheels[i].symbols[j] = wheelSymbols[j];
}
}
}
// Function to generate a random position in each wheel
void spinWheels(slotMachine fwheels[3])
{
time_t seed;
time(&seed);
srand(seed);
for (int i = 0; i < 3; i++)
{
fwheels[i].spinPos = (rand() % WHEEL_POSITIONS);
}
}
void displayResults(slotMachine fwheels[3])
{
for (int i = 0; i < 3; i++)
{
fwheels[i].spinSymbol = fwheels[i].symbols[(fwheels[i].spinPos)];
cout << fwheels[i].spinSymbol;
}
cout << endl;
}
bool getWinner(slotMachine fwheels[3])
{
if ((fwheels[0].spinSymbol == fwheels[1].spinSymbol) && (fwheels[0].spinSymbol == fwheels[2].spinSymbol) && (fwheels[1].spinSymbol == fwheels[2].spinSymbol))
{
return true;
}
else
{
return false;
}
}
我确信我错过了一些简单的东西,但我找不到!
每次调用函数spinwheels
时,都会重新设定随机数生成器的种子。
将这三行移动到main
函数的顶部。
time_t seed;
time(&seed);
srand(seed);
当我们使用rand()
生成随机数时,我们实际上使用的是伪随机数生成器(PRNG),它基于称为seed
的特定输入生成一个固定的随机值序列。当我们设置种子时,我们有效地重置了序列,使其再次从同一种子开始。
您可能会认为,使用time
每次都会产生不同的种子,这仍然会每次都给您带来不同的结果,但在快速计算机程序中,时间过得太少,以至于每次调用时种子实际上都没有变化。
这就是为什么,正如另一个答案所提到的,您应该在程序中只调用srand()
一次。
您应该在程序中只调用srand()
一次,而不是每次都要生成随机数。
如果你在短时间内通过time()
重新设定rand()
的种子,你最终只会重新启动序列,并重复获得第一个值。
相关文章:
- 使用fstream对txt文件中的数字进行循环
- 在 c++ 中查找字符串中没有循环的数字总和
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- C++ for 循环,用于反转数字 10 - 99
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 如何使用循环对所有数字求和?
- 循环中的线程 Runnin 用于对 c++ 中的数字求和
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- C++在不使用pow或循环的情况下计算一个数字的幂
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 如果输入数字为负数,如何循环
- 如何循环开关语句直到找到所需的数字?
- 如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
- C++ 循环快乐数字 CS101.1X
- 在循环中使用字符串流从几个字符串中提取数字
- 如何制作 arduino 串行循环(连续接收数字)
- 使用 while () 循环从总和中减去数字
- 有什么方法可以在 c++ 中组织"cycling"(循环)数字?
- 我的堆栈使用链表时出错.无法打印数字循环.(C++)