Troubleshooting Code: Counting A Wins Over B

AI Thread Summary
The code attempts to count wins for A over B by randomly selecting between the two a million times in a loop, but it suffers from performance issues due to excessive calls to srand() and unnecessary function calls. The inner loop runs one billion times, which can cause the program to take too long to execute, leading to a blank output. Suggestions include moving srand() outside the loops and simplifying the win-checking logic by using a direct comparison instead of a function call. Additionally, a user inquired about accounting for a 15% error rate in the results, indicating a need for adjustments in the code to handle potential misclassifications. Overall, optimizing the code structure and logic will enhance performance and clarity.
magnifik
Messages
350
Reaction score
0
can someone tell me what is wrong with my code? i am trying to make a code that has 1000000 randomly choosing A or B 1000 times. if A is bigger than B, i want to increment the variable called 'aWins'... when i try to get it to print the screen is blank. i left out the int main and #include parts but i have them in my real code...

Code:
     for (int j = 0; j < 1000; j++){
        for (int i = 0; i < 1000000; i++){
		srand(time(0));
		if ((rand()%2) == 0)
			A++;
		else
			B++;
	}
	if (aWins(A,B))
		aWins++;
        }
        cout << aWins << endl;

   // this is my aWins function
    bool aWins(int x, int y){
	if (x > y)
		return true;
	else
		return false;
    }
 
Physics news on Phys.org
Could you post the entire program, so I can run it? Just by looking at it, I see nothing wrong.

As a side note, you can rewrite your function as:

Code:
 bool aWins(int x, int y){
	return(x > y);
    }

Or simply use that Boolean expression in place of the function with a simple comment beside it to denote its meaning:
Code:
if (A > B) //If A "wins" aka is bigger
   aWins++;

Though, I think anyone would know what you were doing without a comment or a function as long as you defined the purpose of the program at the top.

I would also use how integers can behave like a Boolean:
Code:
if ( !(rand()%2) )
   A++;
else
   B++;
Or reverse the order to minimize the not gates:
Code:
if ( (rand()%2) )
   B++;
else
   A++;
 
Your inner loop is running one billion times, and in each iteration you're calling srand(). There's no need to call this function so many times; once will be fine. It seems likely to me that the reason you're not getting output is that your program is taking longer than necessary to do its calculations.

Also, as xcvxcvvc noted, it's much simpler to just compare A and B in an if statement using <. You have the overhead of making a function call 1000 times. Moving srand out of both loops and getting rid of aWins function should speed up your program noticeable.
 
ok, thanks. it's still a little slow, but it's not as bad as before.
 
Last edited:
i have another question..what if there was a 15% error in which B was mistaken for A or vice versa (without taking into account error, 520000 chose A while 480000 were for B). how would i account for this in my code?
 
Back
Top