C ++使用srand(time(0))猜测一个范围内的数字

时间:2014-08-07 23:38:03

标签: c++ random range srand

好吧,所以这里是我的问题......我几乎完成了我制作的小游戏,但是我想测试它是否适用于“调试作弊”以查看程序是否实际显示的是正确的数字。

我设置了srand(time(0)),这样当你玩游戏时,程序显然每次都会使数字选择不同。但是,显然它显然会产生不同的数字,因为系统时间正在发生变化。

正如您在代码中看到的,我有一个名为“ANSWER”的变量,也就是Range(低,高)。检查名为“guessN”的用户输入变量以查看它是否与“ANSWER”匹配。如果确实如此,那么程序会说用户赢了比赛,我相信这很有用。

问题是当我想让程序首先显示答案是什么时,恰好是47,当我输入47时,它实际上是错误的,因为时间生成器仍在继续它已经变成了37岁。

我需要帮助的是什么:有没有办法暂停生成,直到用户输入他的答案?...

感谢您提前帮助我! :)

屏幕拍摄:

http://i.imgur.com/mVRSWCU.png

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib> // For srand and rand

using namespace std;

/*Guessing Game: Program generates random #'s,
user has 3 chances to guess one number at a time,
otherwise the Computer wins.
*/


static unsigned int rangevalue;

void NewRange(int nLow, int nHigh) // Range for generated #'s.
{
    rangevalue = (rand() % (nHigh - nLow + 1)) + nLow;
}


unsigned int getRange() 
{
    return rangevalue;
}

//DON'T GET LOST!
string DescRange; // Descriptive Range
string SelectionInput;
int low = 1;
int high;
//DON'T GET LOST!

void NumberGen()
{



    do
    {
        cout << "Guess a number in between...\n " << endl;
        cout << "1.) 1-5\t\t[EASY]" << endl;
        cout << "2.) 1-10\t[MEDIUM]" << endl;
        cout << "3.) 1-50\t[HARD]" << endl;
        cout << "4.) 1-100\t[IMPOSSIBLE]" << endl;
        cout << "\n5.) Choose a custom range? *" << endl;
        cout << "6.) EXIT \n" << endl;
        cout << "=> ";
        getline(cin, SelectionInput);

    } while (
           SelectionInput != "1" && SelectionInput != "2" && SelectionInput != "3" && SelectionInput != "4" && SelectionInput != "5" && SelectionInput != "6"
        && SelectionInput != "EASY" && SelectionInput != "Easy" && SelectionInput != "easy"
        && SelectionInput != "MEDIUM" && SelectionInput != "Medium" && SelectionInput != "medium"
        && SelectionInput != "HARD" && SelectionInput != "Hard" && SelectionInput != "hard"
        && SelectionInput != "IMPOSSIBLE" && SelectionInput != "Impossible" && SelectionInput != "impossible"
        && SelectionInput != "CUSTOM" && SelectionInput != "Custom" && SelectionInput != "custom" 
        && SelectionInput != "EXIT" && SelectionInput != "Exit" && SelectionInput != "exit"
            );



    if (SelectionInput == "1" || SelectionInput == "EASY" || SelectionInput == "Easy" || SelectionInput == "easy")
    {
        high = 5;
        DescRange = "1-5";

        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "2" || SelectionInput == "MEDIUM" || SelectionInput == "Medium" || SelectionInput == "medium")
    {
        high = 10;
        DescRange = "1-10";
        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "3" || SelectionInput == "HARD" || SelectionInput == "Hard" || SelectionInput == "hard")
    {
        high = 50;
        DescRange = "1-50";
        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "4" || SelectionInput == "IMPOSSIBLE" || SelectionInput == "Impossible" || SelectionInput == "impossible")
    {
        high = 100;
        DescRange = "1-100";
        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "5" || SelectionInput == "CUSTOM" || SelectionInput == "Custom" || SelectionInput == "custom")
    {

        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

        cout << "Input a digit range for you to guess a number in between. " << endl;

        cout << "Low: ";
        cin >> low;

        cout << "High: ";
        cin >> high;

    }

    else if (SelectionInput == "6" || SelectionInput == "EXIT" || SelectionInput == "Exit" || SelectionInput == "exit")
    {

        high = 10;

        cout << "\nYou've chosen to terminate the program!" << "\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;
        for (int iii = 0; iii < 5; iii++)
        {
            cout << "The program will now collapse..." << endl;

        }

        cout << "\n" << endl;

    }



    NewRange;

    int ANSWER = getRange();


    cout << "For debugging/test purposes, the range is: \"" << ANSWER << "\". \n" << endl; // DEBUG for knowing what the 'ANSWER' is.


}



void Guessing()
{


    NumberGen();

    int ANSWER = getRange(); // Making things SIMPLE
    int guessN;

    if (SelectionInput == "5" || SelectionInput == "CUSTOM" || SelectionInput == "Custom" || SelectionInput == "custom")
    {

        cout << "Guess the number I picked between " << low << "-" << high << ": ";


        cin >> guessN;

        if (guessN == ANSWER)
            cout << "Oh no, you won! The answer \"" << guessN << "\", is... correct! \n\n" << endl;

        else
            cout << "\nSorry you lose, try again! Answer was " << ANSWER << ". \n\n" << endl;



    }

    else
    {
        cout << "Guess the number I picked between " << DescRange << ": ";

        cin >> guessN;

        if (guessN == ANSWER)
            cout << "Oh no, you won! The answer \"" << guessN << "\", is... correct! \n\n" << endl;

        else
            cout << "\nSorry you lose, try again! Answer was "<<ANSWER<<". \n\n" << endl;


    }



}


int main()
{

    srand(time(0));

    Guessing();







//system("PAUSE");      //  * Temporarily Disabled *

    return 0;

}

1 个答案:

答案 0 :(得分:-1)

您应该只在程序中执行一次srand(time(0)) - 在调用其他代码之前将其移至main()程序。

srand(time(0))通常被认为是一个不好的主意,安全性是一个问题,但这是一个不同的主题,这里讨论的时间太长了。

修改

代码的最简单修复是创建两个函数 - 一个用于生成新值,另一个用于检索值;

static unsigned int rangevalue;
void NewRange(int nLow, int nHigh) // Range for generated #'s.
{
    rangevalue = (rand() % (nHigh - nLow + 1)) + nLow;
}
unsigned int getRange() {
    return rangevalue;
}

然后在代码中使用这两个函数。

如果你想让你的代码更像C ++,,可以考虑使用类概念来包装逻辑......就像这样;

class RangeValues {
    unsigned int rvalue;
public:
    RangeValues() {
       static bool initialized = false;
       if (!initialized) {
          initialized = true;
          srand(time(0));
       }
       rvlaue = rand();
    }
    unsigned getRange(int nLow, int nHigh) { // Range for generated #'s.
        return (rvalue % (nHigh - nLow + 1)) + nLow;
    }
}

每次创建RangeValues的新实例时都会生成一个新值,然后您可以通过引用传递它。