每当我在我的类文件中调用一个特定的方法时,我会返回一个随机值,但是它会在每次再次调用该方法时返回相同的值。兰德()每次函数调用返回相同的值
例如
int CommuterTrain::getRandNumber(int maximumValue)
{
srand(unsigned(time(NULL)));
int maximum = maximumValue;
return rand()%maximum+1;
}
void CommuterTrain::loadRiders()
{
int passengers = getRandNumber(350);
currentRiders += passengers;
if (currentRiders > maxCapacity) {
cout << "The train has reached capacity! \nSome people were left at the station."
<< endl;
currentRiders = maxCapacity;
}
else {
cout<<passengers<<" pax have entered the vessel"<<endl;
}
}
假设发电机产生数215人以上成行。当我再次调用该方法时,它不会再次随机化,每次都以215结束。
问题在发生器中,还是在下面的方法?
只给发生器播种一次 – paulm
为什么每次都播种兰特?在程序启动时你应该这样做。 – Linuxios
Soooooooooooooooo许多重复... –