CS1101C Lab 3 (Odd Week)

Intelligent Number Guesser

The deadline for this lab question is Wednesday 12 October 2005, 23:59:59 hours.

The name of your C program file must be called guess.c, files with any other name will not be marked.

Background

Nemo and Dory are at it again. You are a born-again shark who has gone vegetarian. You are swimming nearby and you eavesdrop on their conversation:

Dory: “Let’s play a number guessing game.”
Nemo: “Err...”
Dory: “Come on, it’ll be lots of fun!”
Nemo: “Ok ok... what is it?”
Dory: “I’ll think of a secret random number between 1 too 100 inclusive, and you have to guess it. After each guess, I’ll tell you whether it is too high, too low, or correct. We’ll see how many guesses you’ll take.”
Nemo: “Ok... but no cheating ok?”
Dory: “Ok, promise. I’ve thought of a random number. Shoot!”
Nemo: “50.”
Dory: “Too low.”
Nemo: “75.”
Dory: “Too high.”
Nemo: “63.”
Dory: “Too low.”
Nemo: “69.”
Dory: “Too high.”
Nemo: “66.”
Dory: “Too high.”
Nemo: “65.”
Dory: “Correct! You took 6 guesses. This is so much fun. Shall we play again?”
Nemo: “Hey, I know of a way that we can speed things up! I have a shark friend who is learning Programming Methodology and he/she can write a program for us! For each experiment, we just tell him/her the random number seed we want, minimum and maximum possible values of the secret number, and the number of simulations. The program can then tell us the average number of guesses for the experiment. Is that cool or what?”
Dory: “Cool...”

As proof of your born-again behavior, you step out from the shadows. After giving them the fright of their life, as Nemo’s friend, you offer your services once again.

Intelligent Guessing

If you examine Nemo’s pattern of guessing more closely, you realize that he is not as dumb as he looks. His first guess is:

Since this was too low, his second guess is:

Since this was too high, his third guess is:

And so on. Do you see a pattern? Identify it and think about how you would write the algorithm for intelligently guessing a random integer.

Implementation

You will be provided with an input file consisting of different experiment scenarios. Each line in the input file gives the parameters for a single experiment. Each experiment typically contains many simulations (100, 1,000, or possibly more). A typical line from the input file consist of four integers separated by blank spaces as shown below:
122 1 100 1000
The explanation for each number is given as follows:

122Random number seed
1Minimum value for secret number
100Maximum value for secret number
1000Number of simulations

This says that for this particular experiment, the random number seed is 122, the minimum value for the secret number is 1, the maximum value for the secret number is 100, and we wish to carry out 1,000 simulations.

An Experiment

We illustrate how the experiment is performed for the above. First, we initialize the random number seed to 122. The random number seed should be initialized only at the beginning of each experiment; not in between simulations. For the above experiment, we need to conduct 1,000 simulations.

A Simulation

We illustrate how a single simulation is performed using the parameters given above. First, we choose a secret number which is a random integer between 1 to 100 inclusive. Then, we keep simulating Nemo’s guesses by intelligently guessing the secret number. We also simulate Dory’s truthful responses (too high / too low / correct) to Nemo. We take note of the number of guesses Nemo takes to guess each secret number.

Experiment Data

After running 1,000 simulations, we calculate the average number of guesses taken by Nemo to guess each random secret number correctly, and display it rounded off to three decimal places.

Input File

Assume that the text input file is always called number1.txt. This sample input file contains data for seven experiments:

121 1 100 100
122 1 100 1000
123 1 100 10000
123 2 101 10000
123 -49 50 10000
124 1 1000 10000
125 1 10000 10000

We will test your program with other experiment data, so your program must work for the general case, and not just only for the sample input file given.

Sample Run

The following shows a sample run for the input file numbers1.txt. Actual values for the average number of guesses have been replaced with x’s.

You are reminded to follow the sample outcome exactly, else marks will be deducted.

Seed: 121, min: 1, max: 100, average number of guesses: x.xxx.
Seed: 122, min: 1, max: 100, average number of guesses: x.xxx.
Seed: 123, min: 1, max: 100, average number of guesses: x.xxx.
Seed: 123, min: 2, max: 101, average number of guesses: x.xxx.
Seed: 123, min: -49, max: 50, average number of guesses: x.xxx.
Seed: 124, min: 1, max: 1000, average number of guesses: x.xxx.
Seed: 125, min: 1, max: 10000, average number of guesses: xx.xxx.

Take Note

Tips

Ponder

What is the relationship between (max - min + 1) and the average number of guesses?


This document, index.html, has been accessed 17 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 11 different hosts have accessed this document in the last 445 days; your host, 216.73.216.39, has accessed it 1 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.