patterncppMinor
Reverse "Guess the Number" program
Viewed 0 times
numberthereverseprogramguess
Problem
I recently got to a certain exercise in a book I am reading, and am looking for feedback. It is a program in which the user picks a number and the computer uses algorithms to guess it. If there is any way that I can improve my code, feedback is really appreciated!
#include
#include
#include
#include
using namespace std;
int main()
{
srand(static_cast(time(0))); //seed random number generator
int compNumber = rand() % 100 + 1;
int yourNumber;
cout > yourNumber;
cout yourNumber)
{
do
{
--compNumber;
} while (compNumber != yourNumber);
}
else if (compNumber > yourNumber)
{
do
{
++compNumber;
} while (compNumber != yourNumber);
}
}
cout << "I guessed it! Your number is " << compNumber << endl;
return 0;
}Solution
Sure this generates a random number from 1 -> 100.
BUT not all the numbers have an even probability. Assuming RAND_MAX is 32768 (a common value). Then the number 1->68 have a slightly higher probability than the number 69->100.
To get an even distribution you need to compensate for this:
Better yet learn to use the modern random number generator that is built into C++.
This does not look correct:
The conditions look the same to me.
This does not look like guessing.
This looks like the computer counting down until it reaches your number.
int compNumber = rand() % 100 + 1;BUT not all the numbers have an even probability. Assuming RAND_MAX is 32768 (a common value). Then the number 1->68 have a slightly higher probability than the number 69->100.
To get an even distribution you need to compensate for this:
int compNumber
do
{
compNumber = rand();
}
while (compNumber > (RAND_MAX / 100 * 100));
compNumber = compNumber % 100 + 1;Better yet learn to use the modern random number generator that is built into C++.
// I have not tested this.
// Just copied and pasted from: http://stackoverflow.com/a/19666713/14065
// This is the modern equivalent of srand()
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution dist(1, 100);
// Then usage is.
// equivalent of rand().
std::cout << dist(mt) << "\n";This does not look correct:
if (compNumber > yourNumber)
{}
else if (compNumber > yourNumber)
{}The conditions look the same to me.
This does not look like guessing.
do
{
--compNumber;
} while (compNumber != yourNumber);This looks like the computer counting down until it reaches your number.
Code Snippets
int compNumber = rand() % 100 + 1;int compNumber
do
{
compNumber = rand();
}
while (compNumber > (RAND_MAX / 100 * 100));
compNumber = compNumber % 100 + 1;// I have not tested this.
// Just copied and pasted from: http://stackoverflow.com/a/19666713/14065
// This is the modern equivalent of srand()
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<int> dist(1, 100);
// Then usage is.
// equivalent of rand().
std::cout << dist(mt) << "\n";if (compNumber > yourNumber)
{}
else if (compNumber > yourNumber)
{}do
{
--compNumber;
} while (compNumber != yourNumber);Context
StackExchange Code Review Q#115371, answer score: 7
Revisions (0)
No revisions yet.