重複の可能性:
範囲からランダムな整数を生成する
ユーザーが頭に浮かんだ数字をコンピューターが推測するプログラムを作成しようとしています。必要なユーザー入力は、推測が高すぎる、低すぎる、または正しいかどうかだけです。以前の推測に基づいて最小値と最大値を格納する 2 つの変数の間で乱数を生成する際に問題が発生しています。これが私のコードです:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast <unsigned int> (time(0)));
int compGuess = rand() % 100 +1; //Generates number between 1 - 100
int highestNumber = 100;
int lowestNumber = 1;
char ready;
char highLowSuccess;
bool success;
int tries = 0;
cout << "Please pick a number between 1 - 100. I will guess your number. Don't tell me what it is!\n\n";
do
{
cout << "Are you ready? (y/n)\n\n";
cin >> ready;
if (ready == 'y')
{
do
{
cout << "Is your number " << compGuess << "?\n\n";
cout << "High, Low or Success?";
++tries;
cin >> highLowSuccess; //User input telling the computer whether its too high, too low, or a success
if (highLowSuccess == 'h') //Executes code if number guessed was too high.
{
highestNumber = compGuess - 1; //Stores variable indicating the highest possible number based on user input
compGuess = rand() % highestNumber +1; //Generates a new random number between 1 and the new highest possible number
success = false;
}
else if (highLowSuccess == 'l') //Executes code if number guessed was too low.
{
lowestNumber = compGuess + 1;//Stores variable indicating the lowest possible number based on user input
compGuess = (rand() % highestNumber - lowestNumber + 1) + lowestNumber // <---- Not producing the desired result
success = false;
}
else if (highLowSuccess == 's') //Executes code if the computer's guess was correct.
{
cout << "I guessed your number! It only took me " << tries << " tries!";
success = true;
}
} while (success != true);
}
else
{
continue;
}
} while (ready != 'y');
return 0;
}
maximumNumber は最大値であり、lowestNumber は最小値です。可能な最大値と最小値を考慮しながら乱数を生成できる方程式が必要です。
答えが本当に単純な場合は許してください。私は初心者プログラマーです。xD