兰特数倾斜。 (C++)
可能的重复:
srand()、C++ 的问题
,我生成的随机数的大小是倾斜的,每次在我的 while 循环中使用它时。它是随机的,不应该倾斜,也不应该是随机的。 变量 hit 由随机数定义。但正如我所说,随机数不是随机的。此外,随着随机数变大,它会超出我为其设置的限制。
//Code im using for random number :
srand((unsigned)time(0));
hit = rand()%15;
//The subject for the problem :
while (1) {
if (mon1==0) {
cout << "A dragon appears." << endl;
system ("pause");
cout << "Enter 1 to attack and 2 to run away, " <<name << endl;
cin >> act;
if (act==2) {
cout << "You run away. " << endl;
}
if (act==1) {
cout << " You choose to attack! Good luck " << endl;
}
}
if (mon1==1) { // orc
srand((unsigned)time(0));
hit = rand()%15;
ehealth = (100);
cout << " A orc appears" << endl;
system ("pause");
cout << "Enter 1 to attack and 2 to run away, " <<name << endl;
cin >> act;
system ("cls");
if (act==2) {
cout << "You run away. " << endl;
system ("cls");
}
if (act==1) { // ATTACK HERE MAIN ATTACk TESING !!!!!!!!!!!!!!!!!!!!!!! ORC
cout << " You choose to attack! Good luck " << endl;
cout << " Your health is " << health << endl;
cout << " The enemys health is " << ehealth << endl;
cout << " You have 3 abilitys." << endl;
while (1) {
cout << " Press 1 to swipe, 2 to block the enemys attack and 3 to use your special attack" <<endl;
cin >> act1;
system ("cls");
if (act1==1) {
cout << " You swipe the enemy " << endl; // swipe
cout << " You hit for " << hit << endl;
if (wep==1) {
hit = (hit + 4);
cout << " As you are the axe i will add on 4 to that, making " << hit << endl; // axe
}
ehealth = (ehealth - hit);
cout << " The enemys health is " << ehealth << endl;
cout << " Your health is " << health << endl;
system ("pause");
}
if (act1==2) {
cout <<" You block, making you immune to the next attack" << endl; //block
ehit = (0);
//newrand
cout << " Your health is " << health << endl;
cout << " The enemys health is " << ehealth << endl;
}
if (act1==3) {
cout << " Special attack is being worked on BUDDY, GUY, FRIEND!! " << endl;
}
system ("pause");
if (health < 0) {
cout << "You died, sorry!" << endl;
}
if (ehealth < 0) {
cout << " Yay! You killed the enemy!" << endl;
}
if (health == 0) {
cout << "You died, sorry!" << endl;
}
if (ehealth == 0) {
cout << " Yay! You killed the enemy!" << endl;
}
cout << " Now it is the enemys turn! " << endl;
health = ( health - ehit );
cout << " The enemy hits you for a " << ehit << endl;
cout << " Your health is now " << health << endl;
srand((unsigned)time(0));
ehit = rand()%10;
system ("pause");
system ("cls");
}
// END OF ORC
}
}
Possible Duplicate:
Problems with srand(), C++
Basically, the random number I am generating is inclining in size, everytime it is used inside my while loop. It being random, should't incline and not be random as such.
The variable hit is defined by the random number. But as I said, the random number is not random. Also, as the random number gets bigger it goes over the limit I set for it.
//Code im using for random number :
srand((unsigned)time(0));
hit = rand()%15;
//The subject for the problem :
while (1) {
if (mon1==0) {
cout << "A dragon appears." << endl;
system ("pause");
cout << "Enter 1 to attack and 2 to run away, " <<name << endl;
cin >> act;
if (act==2) {
cout << "You run away. " << endl;
}
if (act==1) {
cout << " You choose to attack! Good luck " << endl;
}
}
if (mon1==1) { // orc
srand((unsigned)time(0));
hit = rand()%15;
ehealth = (100);
cout << " A orc appears" << endl;
system ("pause");
cout << "Enter 1 to attack and 2 to run away, " <<name << endl;
cin >> act;
system ("cls");
if (act==2) {
cout << "You run away. " << endl;
system ("cls");
}
if (act==1) { // ATTACK HERE MAIN ATTACk TESING !!!!!!!!!!!!!!!!!!!!!!! ORC
cout << " You choose to attack! Good luck " << endl;
cout << " Your health is " << health << endl;
cout << " The enemys health is " << ehealth << endl;
cout << " You have 3 abilitys." << endl;
while (1) {
cout << " Press 1 to swipe, 2 to block the enemys attack and 3 to use your special attack" <<endl;
cin >> act1;
system ("cls");
if (act1==1) {
cout << " You swipe the enemy " << endl; // swipe
cout << " You hit for " << hit << endl;
if (wep==1) {
hit = (hit + 4);
cout << " As you are the axe i will add on 4 to that, making " << hit << endl; // axe
}
ehealth = (ehealth - hit);
cout << " The enemys health is " << ehealth << endl;
cout << " Your health is " << health << endl;
system ("pause");
}
if (act1==2) {
cout <<" You block, making you immune to the next attack" << endl; //block
ehit = (0);
//newrand
cout << " Your health is " << health << endl;
cout << " The enemys health is " << ehealth << endl;
}
if (act1==3) {
cout << " Special attack is being worked on BUDDY, GUY, FRIEND!! " << endl;
}
system ("pause");
if (health < 0) {
cout << "You died, sorry!" << endl;
}
if (ehealth < 0) {
cout << " Yay! You killed the enemy!" << endl;
}
if (health == 0) {
cout << "You died, sorry!" << endl;
}
if (ehealth == 0) {
cout << " Yay! You killed the enemy!" << endl;
}
cout << " Now it is the enemys turn! " << endl;
health = ( health - ehit );
cout << " The enemy hits you for a " << ehit << endl;
cout << " Your health is now " << health << endl;
srand((unsigned)time(0));
ehit = rand()%10;
system ("pause");
system ("cls");
}
// END OF ORC
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
命中
在程序启动时是随机的。如果怪物是兽人,
命中
会重新随机。每次你用斧头附加时,攻击力永久增加四。
您可能还想在用户每次攻击时重新随机化
命中
?hit
is randomized when the program starts.If the monster is an orc,
hit
is re-randomized.Every time you attach with an axe, hit permanently increases by four.
You probably want to re-randomize
hit
each time the user attacks as well?此代码会导致此行为:
您总是将命中增加 4。因此您应该使用临时变量。
This code causes this behavior:
You increase hit always by 4. So you should use a temporary variable instead.
您只需在程序开始时调用 srand 一次。这会为随机数生成器“播种”之后无限数量的 rand() 调用 - 多次调用它只会使随机数变得不随机,正如您遇到的那样。
You only need to call srand once, at the start of the program. That "seeds" the random number generator for an unlimited number of rand() calls afterwards - calling it multiple times will just make the random numbers not random, as you encountered.