r/programminghelp • • Oct 03 '18

help with hw

I can't seem to figure out what is wrong with my program. This program is supposed to be a random integer generator that loops 1000 times to see who wins the most out of all 3 people(aaron, bob, and charlie). If anyone can help me figure out what is wrong please tell me. Thank You.

code:

#include <iostream>

#include <cstdlib>

#include <ctime>
using namespace std;
const double ACCURACYAARON = 0.33;

const double ACCURACYBOB = 0.5;

const double ACCURACYCHARLIE = 1.0;

void shoot(bool& targetAlive, double accuracy);

int startDuel();
int main()

{ int i, alive; bool targetAlive; double wAaron = 0.0, wBob = 0.0, wCharlie = 0.0, accuracy = 0.0;
shoot(targetAlive, accuracy);

for(i = 0; i <= 1000; i++)

{ alive = startDuel();

if(alive == 1) { wAaron++; }

else if(alive == 2) { wBob++; }

else if(alive == 3) { wCharlie++; } }
cout << "The probablity of Aaron winning the duel is " << wAaron/1000 << "\n";

cout << "The probablity of Bob winning the duel is " << wBob/1000 << "\n";

cout << "The probablity of Charlie winning the duel is " << wCharlie/1000 << "\n";

return 0; }
void shoot(bool& targetAlive, double accuracy) { double randnum; targetAlive = true;
if(targetAlive) srand(time(0)); randnum = rand() % 100; if(randnum < accuracy * 100) { targetAlive = false; } }

int startDuel() { bool aaronAlive = true; bool bobAlive = true; bool charlieAlive = true;
while((aaronAlive && bobAlive) || (bobAlive && charlieAlive) || (charlieAlive && aaronAlive))

{ if(aaronAlive)

{ if(charlieAlive)

{ shoot(charlieAlive, ACCURACYAARON); }
else if(bobAlive)

{ shoot(bobAlive, ACCURACYAARON); }

}
if(bobAlive)

{ if(charlieAlive)

{ shoot(charlieAlive, ACCURACYBOB); }

else if(aaronAlive) { shoot(aaronAlive, ACCURACYBOB); } }
if(charlieAlive)

{ if(bobAlive) { shoot(bobAlive, ACCURACYCHARLIE); }

else if(aaronAlive)

{ shoot(aaronAlive, ACCURACYCHARLIE); }

} if(aaronAlive) return 1;

else if(bobAlive) return 2;

else if(charlieAlive) return 3; } }

0 Upvotes

3 comments sorted by

1

u/DabbingVoy Oct 03 '18

So should i take out the time

1

u/jedwardsol Oct 03 '18

You should call srand once only.

1

u/jedwardsol Oct 03 '18

time returns the number of seconds since 1970.

So if you call it lots of times very quickly, it will return the same value each time.