嗨我正在寻找RandNum来生成2-10之间的数字,然后该数字从15开始。
现在,每次执行程序时,从15(PerseusHealth)中取出的数字是7.如何解决此问题,并使其随机化?
void desertpath()
{
int scorpianchoice; //the option the player chooses.
int Maxhit = 10; //Max hit the scorpian can do
int Minhit = 2; //Min hit the scorpian can do
int randNum = rand()%(Maxhit + Minhit) + Minhit;
int healthremaining = PerseusHealth - randNum; //health left in option1.
if(scorpianchoice == 1)
{
cout << "You run under the Scorpians Legs in hopes to escape " << endl;
cout << "You are hit by the scorpians Sting for " << randNum << " hp!";
cout << "You have " << healthremaining << "/15 HP Left!" << endl;
cout << "You escape the Scorpian but not without taking some damage" << endl;
}
答案 0 :(得分:4)
使用srand初始化。
srand((unsigned)time(0));
另外,我认为您没有正确放置括号:
int randNum = (rand()%(Maxhit - Minhit)) + Minhit;
答案 1 :(得分:0)
在@ ebad86回答之上澄清
rand()因质量差而闻名,特别是在使用低位时。例如,最有可能的是,输出总是在低位设置为1的情况下生成。简单的解决方案可能是通过移位(例如,按8)来使用序列中间的位
如果rand()的最大生成输出不能被(Maxhit-Minhit)整除,则会得到(稍微)偏差的结果。它可能适合您的目的,但您最好知道它