-1

I had a lab where we had to write a program to simulate the duel, for Aaron who a has probability of 1/3, Bob 1/2, Charlie never misses. The program should use random numbers and the probabilities given in the problem to determine whether a shooter hits his target, simulate 10,000 duels.

First time with rand and having some problems with my functions, my professor told me to put the %accuracy so you would only need one function and it would input the players accuracy from the function call, but I am getting an error: invalid operands of types int' anddouble' to binary `operator%', I am confused because accuracy is a number.

I am also having trouble with the while loop because 'fighters' should only subtract one when a player is shot and there will be times when a player is not shot.

#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;

}           
Csci319
  • 125
  • 8
  • 4
    The compiler is telling you `operator%` doesn't work on doubles. You're giving it a double. You need [`modf`](http://en.cppreference.com/w/cpp/numeric/math/modf). Next time post a short example that reproduces the problem, not *all* your code. – Borgleader Oct 09 '14 at 18:00
  • @Borgleader He doesn't need `modf`; it's quite simple to convert the results of `rand()` into a floating point value in the range `[0.0, 1.0)`, and compare that to accuracy. – James Kanze Oct 09 '14 at 18:06
  • @JamesKanze By "need modf" clearly I meant to apply a modulo operation on arguments of type double... I don't claim to know what OP is trying to achieve with this code. – Borgleader Oct 09 '14 at 18:07
  • By "Simple" you mean "The Committee had to give everyone a random library to do it correctly because nobody could get it right"? – Puppy Oct 09 '14 at 18:10
  • You (almost certainly) only want to call `srand` once (e.g., at the beginning of `main`) not every time you get another random number the way you've done here. – Jerry Coffin Oct 09 '14 at 18:20
  • Thanks, so would the function look something like this? double rand1 (double accuracy){ double r = rand (); if ( (r / RAND_MAX) < accuracy){ return r; } } – Csci319 Oct 09 '14 at 19:00
  • @BS319 That looks close to what you want. – James Kanze Oct 10 '14 at 08:20
  • @JamesKanze thanks. I am trying to figure out the while-loop so fighters only subtracts one if a fighter gets shot, for some reason I cannot figure it out – Csci319 Oct 10 '14 at 21:22

2 Answers2

0

You can't use modulo with a double. There are several ways of handling this, but if the accuracy is always 1 over a smal integer a, you can do rand() % a == 0 to see if it hits. Otherwise, you should first convert the results of rand() into a double in the range [0.0, 1.0), by rand() / (RAND_MAX + 1.0, and then if the specified accuracy is larger than that, it is a miss.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0
double rand1 (double accuracy){
   srand(time(0));
   int a = rand ()% accuracy; <-- THIS
   double b = ((double)a / RAND_MAX);
   return b;
} 

I'm not sure what you think that line does, but it doesn't make any sense. If you want to keep the accuracies as fractions, use:

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

Also, don't call srand more than once. Otherwise, two calls to rand1 in the same second will get the same return value from rand.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Thanks, so would the function look something like this? double rand1 (double accuracy){ double r = rand (); if ( (r / RAND_MAX) < accuracy){ return r; } } – Csci319 Oct 09 '14 at 18:39
  • I'd suggest returning a bool, so `return (r / RAND_MAX) < accuracy;`. – David Schwartz Oct 09 '14 at 20:49
  • thanks so much. It is working now. I just can't figure out how to fix the while loop. any suggestions? – Csci319 Oct 09 '14 at 21:58
  • You just need to fix it. For example, don't let fighters shoot if they're dead. Also, don't create two variables both named `aaron`, and so on for the other fighters. – David Schwartz Oct 09 '14 at 22:13
  • I really appreciate your help, I am new to this. I thought my if-statements in the functions make sure if they are shot they wont shoot. My while-loop is to see if someone was hit and if so then fighters--; thats what I need help with – Csci319 Oct 09 '14 at 23:13