I’m working on a computer player for a number guessing game but I’m stuck on a problem. The computer needs to guess the player’s number between 1 and 100. My approach is to generate random numbers and narrow down the range based on whether the guess is too high or too low.
The logic seems right and it adjusts the bounds correctly, but after a few iterations the guess values go way above 100. What am I doing wrong here? Any tips for building better computer players would be great too!
Hit the same random bug when I built an AI player last year. Your range calculation breaks when you update the bounds.
rand() % upperBound + lowerBound doesn’t work - you’re missing the actual range size. With lowerBound at 50 and upperBound at 80, you get rand() % 80 + 50 which spits out numbers up to 129.
Use this instead: rand() % (upperBound - lowerBound + 1) + lowerBound
Move srand outside the function too. Calling it over and over makes your random numbers worse.
Honestly though? Drop the random approach completely. Learned this after watching my AI take 50+ guesses on some numbers. Binary search is smarter and way more predictable.
This video explains binary search perfectly:
Binary search makes your AI look intelligent and consistent. Users hate watching random guessing - they want something that actually feels smart.
Your random number formula’s broken. The problem is rand() % upperBound + lowerBound - this won’t give you the right range.
You need: rand() % (upperBound - lowerBound) + lowerBound
Also, don’t call srand(time(NULL)) inside the function. It’ll mess up your randomness.
But random guessing is pretty inefficient anyway. Try binary search instead - always guess the middle of your current range. You’ll find any number in 7 guesses max.
If you want to get fancy with game AI or test different strategies automatically, check out Latenode. I’ve used it for similar gaming projects where I needed to run thousands of test cases. You can set up automated scenarios, track performance, and create webhooks to log results.
The visual workflow builder makes complex game logic easy without heavy coding.