我正在讀的書叫的解決方案「跳進C++」,即它要我建立一個程序,發現挑選1和100之間的數字隨機一個猜謎遊戲的解決方案,讓用戶猜猜這個數字是多少,並告訴他們他們的猜測是高,低還是正好。 基本上它要我預測下一個隨機數,但我無法想出辦法,因爲我是一個新手。C++程序找出猜謎遊戲
這是猜謎遊戲的代碼:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int x,y;
int rand_range(int low, int high)
{
return rand() % (high - low) + low;
}
int main()
{
int seed = time(NULL);
srand(seed);
int y = rand_range(1, 100);
cout << "Program has picked a no. between 1 to 100... You just make a
guess....\n";
cin >> x;
while(1)
{
if(x == y)
{
cout << "just right\n";return 0;
}
else if(x < y)
{
cout << "low\n";return 0;
}
else
{
cout << "high\n";return 0;
}
}
}
這個程序讓用戶猜1之間的數字爲100,然後檢查猜測是否爲低,高或剛剛好,但我需要一個計劃,解決上述猜測問題並猜測隨機號碼。究竟。 意思是我需要一種方法來預測下一個僞隨機數。
樣品的輸入和輸出,請 –
你需要解釋什麼並不完全工作(你想要什麼,你得到了什麼,而不是)。 – Drop