我怎样才能让rand用准确度给我正确的数字?

时间:2014-10-09 17:58:24

标签: c++ function random while-loop srand

我有一个实验室,我们必须编写一个程序来模拟决斗,对于Aaron来说,他有1/3的概率,Bob 1/2,Charlie从未错过。该程序应使用随机数和问题中给出的概率来确定射手是否击中他的目标,模拟10,000次决斗。

第一次使用rand并且我的功能有些问题,我的教授告诉我把%准确性放在一起,这样你只需要一个函数就可以从函数调用中输入玩家的准确度,但是我收到一个错误: 类型int' and的无效操作数'加倍到二元'运算符%',我很困惑,因为准确性是一个数字。

我在使用while循环时遇到了麻烦,因为'战士'应该只在玩家被击中时减去一个,并且有时玩家没有被射击。

#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>

using namespace std;

double rand1 (double accuracy);
bool aaron_shoot (double a, bool& charlie, bool& bob);
bool bob_shoot(double b, bool& charlie, bool& aaron);
bool charlie_shoot (double c, bool& bob, bool& aaron);

int main() {

 bool aaron, bob, charlie; 
 int aaron_wins = 0, bob_wins = 0, charlie_wins = 0; 
 srand(time(0));

   for ( int battles = 1; battles <= 10000; battles++){

      int fighters = 3; 
      while ( fighters > 1){   
        aaron = aaron_shoot(0.333, charlie, bob);          
        bob = bob_shoot (0.5, charlie, aaron);    
        charlie = charlie_shoot (1.0, bob, aaron); 
         //need an argument to make sure a shooter does not shoot if they are dead, and to count             how many are left if there is a hit
} //keeps track of the win at the end of each round of battles when  
  //there is one player left standing       
       aaron_wins = aaron_wins + aaron;
       bob_wins = bob_wins + bob;         
       charlie_wins = charlie_wins + charlie;          
 }
 cout << "Aaron won " << aaron_wins<< "/10000 duels or " << (aaron_wins/100)<< "%.\n";
 cout << "Bob won " << bob_wins << "/10000 duels or " << (bob_wins/100)<<"%.\n";
 cout << "Charlie won " << charlie_wins << "/10000 duels or " << (charlie_wins/100)<<"%.\n";

system ("Pause"); 
return 0;

}


bool aaron_shoot (double a, bool& charlie, bool& bob){
 if (charlie == true){ //is alive
             if (rand1 (a) >= a){
                       return (charlie = false);                           
                       }
             }
 else if (bob == true){
              if (rand1 (a) >= a){
                        return (bob = false);

                        }
              }          
}

bool bob_shoot (double b, bool& charlie, bool& aaron){
 if (charlie == true){
             if (rand1 (b) >= b){
                       return (charlie = false);

                       }
             }
 else if (aaron == true){
              if (rand1 (b) >= b){
                        return (aaron = false);

                        }
              }      
}   

bool charlie_shoot (double c, bool& bob, bool& aaron){
 if (bob == true){
             if (rand1 (c) >= c){
                      return (bob = false);
                       }
             }
 else if (aaron == true){
              if (rand1 (c) >= c){
                        return (aaron = false);

                        }
              }          
}

double rand1 (double accuracy){
   double r = rand ();            
   return (r / RAND_MAX) < accuracy;

}           

2 个答案:

答案 0 :(得分:0)

您不能将模数与double一起使用。有几种方法 处理这个问题,但是如果精确度总是小于1 整数a,您可以rand() % a == 0查看它是否有效。 否则,您应首先将rand()的结果转换为 范围为double的{​​{1}},[0.0, 1.0), 如果指定的精度大于那个,那就是 想念。

答案 1 :(得分:0)

double rand1 (double accuracy){
   srand(time(0));
   int a = rand ()% accuracy; <-- THIS
   double b = ((double)a / RAND_MAX);
   return b;
} 

我不确定你认为该行是什么,但它没有任何意义。如果您想将精度保持为分数,请使用:

    double r = rand ();
    if ( (r / RAND_MAX) < accuracy)

此外,请勿多次致电srand。否则,在同一秒内对rand1的两次调用将从rand获得相同的返回值。