{"id":3963,"date":"2020-02-01T00:01:01","date_gmt":"2020-02-01T08:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3963"},"modified":"2020-02-09T08:27:10","modified_gmt":"2020-02-09T16:27:10","slug":"automated-guessing-game","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3963","title":{"rendered":"Automated Guessing Game"},"content":{"rendered":"<p>Last month, I published a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3940\">post<\/a> of the classic programming chestnut, a number-guessing game. In the post, I wrote: &#8220;Variable <code>tries<\/code> is set equal to 6 because I read somewhere that it\u2019s possible to guess a value between 1 and 100 in six or fewer tries (though I\u2019m probably incorrect).&#8221; And, yes, I was wrong.<br \/>\n<!--more--><br \/>\nThe true value is seven: When using logic, it takes a maximum of 7 attempts to accurately guess a random value between 1 and 100 (inclusive). This generality got me thinking: If you apply logic to a number-guessing game, you follow a set rule to figure out which number to guess. The rule, or algorithm, works like this:<\/p>\n<ol>\n<li>Divide the range of numbers by two and guess that number (lowest valid number + &half; &times; range)<\/li>\n<li>If the guess is correct, game over, otherwise:<\/li>\n<li>If the guess is high or low, split the range accordingly and repeat at Step 1.<\/li>\n<\/ol>\n<p>For example, you first guess 50. The computer says, &#8220;Too low!&#8221;<\/p>\n<p>You split the difference: 50 to 100 and guess 75. The computer says, &#8220;Too high!&#8221;<\/p>\n<p>You split the difference again: 50 to 75 and guess 63.<\/p>\n<p>And so on.<\/p>\n<p>Your task for this month&#8217;s Exercise is to code this logic: Have the computer generate a random value between 1 and 100. Based on the range, have the computer guess the random value by splitting the difference. The guess is then reported as too high to too low. Repeat the process until the number is discovered, then report how many guesses it took.<\/p>\n<p>Here is a sample run of my solution:<\/p>\n<pre class=\"screen\">Generating a random number 1 to 100...\r\nTry\tNumber\tGuess\tResult\r\n#1\t78\t50\tv low\r\n#2\t78\t75\tv low\r\n#3\t78\t87\t^ high\r\n#4\t78\t81\t^ high\r\n#5\t78\t78\tCorrect<\/pre>\n<p>Above, the computer generated random value 78. It took the computer 5 tries to come up with the correct value.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3972\">Click here<\/a> to view my solution. Before you peek at what I&#8217;ve done, please try this Exercise on your own. And run the test several times to confirm that seven is the maximum number of tries it takes to logically guess a random value between 1 and 100.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have the computer guess its own number. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3963\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3963","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3963"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3963\/revisions"}],"predecessor-version":[{"id":3998,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3963\/revisions\/3998"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}