for循环中的随机数生成器每次都给出相同的数字 [英] Random number generator in a for loop gives same numbers each time
问题描述
该程序应具有非常原始的老虎机功能,可以旋转三个不同的轮子".每个轮子包含一定数量的字符.函数会生成一个随机数,将其分配为每个车轮中的数组位置,然后产生一个与该位置相对应的符号.
This program is supposed to function as a pretty primitive slot machine with three different "wheels" to spin. Each wheels contains a set amount of characters. A function generates a random number to assign as an array position in each wheel, which then yields a symbol corresponding to that position.
我遇到的问题是,在我的for循环的每次迭代中,随机生成的数字都不会改变.因此,对于每个循环,我基本上总是得到"X-X"或"X @-".我搜索了先前提出的问题,并找到了几个相关的问题,但似乎没有一个可以解决我的特殊问题.
The problem I'm having is that the randomly generated number doesn't change through each iteration of my for loop. So I'm basically always getting "X - X" or " X @ -" for every single loop-through. I've searched through previously asked questions and found several related ones, but none seemed to solve my particular problem.
冗长的代码的道歉:
#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;
}
}
我确定这很简单,但是我找不到它!
I'm sure it's something simple that I'm missing, but I can't quite find it!
推荐答案
每次调用函数 swheelwheels
时,您都在重新播种随机数生成器.
You are re-seeding your random number generator every time you call the function spinwheels
.
将这三行移动到 main
函数的顶部.
Move these three lines into the top of your main
function.
time_t seed;
time(&seed);
srand(seed);
当我们使用 rand()
生成随机数时,实际上是在使用伪随机数生成器(PRNG),它根据称为 seed
的特定输入生成固定的随机值序列.设置种子时,我们实际上是在重置序列以再次从同一种子开始.
When we generate random numbers using rand()
, we are actually using a pseudo-Random Number Generator (PRNG), which generates a fixed sequence of random-looking values based on a particular input called a seed
. When we set the seed, we are effectively resetting the the sequence to start at the same seed again.
您可能会认为每次使用 time
都会产生不同的种子,但每次仍然应该给您带来不同的结果,但是在快速的计算机程序中,经过的时间很少,因此每次调用期间种子实际上都不会改变.
You might think that using time
would result in a different seed each time, which should still give you a different result each time, but in a fast computer program, so little time has passed that the seed is effectively unchanged during each call.
这就是为什么,正如另一个答案所提到的,您只应在程序中调用一次 srand()
.
That is why, as the other answer mentions, you should only call srand()
once in your program.
这篇关于for循环中的随机数生成器每次都给出相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!