Troubleshooting Code: Counting A Wins Over B

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a code snippet designed to simulate a game where two entities, A and B, are randomly chosen a million times over a thousand iterations. Participants focus on identifying issues in the code, optimizing performance, and addressing a potential error in the counting mechanism.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant requests the complete code to better diagnose the issue, noting that they see nothing wrong at first glance.
  • Another participant suggests simplifying the aWins function and proposes alternative ways to write the code for clarity and efficiency.
  • Concerns are raised about the inefficiency of calling srand() multiple times within the loop, which may lead to performance issues.
  • One participant notes that the inner loop runs one billion times and suggests moving srand() outside the loops to improve speed.
  • A participant mentions that the program is still slow but has improved after making suggested changes.
  • A new question is posed about how to account for a 15% error in the results, where B could be mistaken for A or vice versa, indicating a need for further adjustments in the code.

Areas of Agreement / Disagreement

Participants generally agree on the inefficiencies in the original code and suggest optimizations, but there is no consensus on how to best implement the error accounting for the 15% mistake in counting.

Contextual Notes

Limitations include the potential misunderstanding of how random number generation affects the results and the implications of introducing error into the counting mechanism, which remains unresolved.

Who May Find This Useful

Readers interested in programming, particularly in random number generation, performance optimization, and error handling in simulations may find this discussion relevant.

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?
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
8
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K