每次调用变量时如何生成随机数?

时间:2014-10-07 08:59:51

标签: c++

我做了一个非常简单的数学导师"程序,我需要显示一个菜单,有4个选项:加法,减法,乘法和退出。程序应循环,直到我选择退出。

到目前为止,所有内容似乎都在起作用,但我无法在每次循环程序时弄清楚如何创建一个新的随机数。

以下是代码:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int addFirstNum,
    addSecondNum,
    subFirstNum,
    subSecondNum,
    multFirstNum,
    multSecondNum,
    operNum,
    num;
    bool temBool = true; //Temporary boolean used in the while loop
    unsigned seed; // Random generator seed

    // Use the time function to get a "seed" value for srand
    seed = time(0);
    srand(seed);

    //Random generated operands
    addFirstNum = rand() % 40 + 10;
    addSecondNum = rand() % 40 + 10;
    subFirstNum = rand() % 40 + 10;
    subSecondNum = rand() % 40 + 10;
    multFirstNum = rand() % 40 + 10;
    multSecondNum = rand() % 40 + 10;


    while (temBool) //Start of the loop
    {
        // Menu of Math Tutor
        cout << "Math Tutor - Main Menu";
        cout << endl << endl;
        cout << "1. Adittion\n";
        cout << "2. Subtraction\n";
        cout << "3. Multiplication\n";
        cout << "4. Exit Program\n";
        cout << endl << endl;
        cout << "Choose your operation to practice (1-4) ";
        cin >> operNum;
        cout << endl << endl;

        // Switch for the menu's options
        switch (operNum)
        {
            case 1:
                cout << "Working with addition\n";
                cout << setw(3) << addFirstNum << "\n"
                << "+" << addSecondNum << "\n"
                << "---\n";
                cout << "Your answer: ";
                cin  >> num;
                if(num == addFirstNum + addSecondNum)
                {
                    cout << "Correct answer: "
                    << addFirstNum + addSecondNum << " Congratulations!\n";
                }
                else
                {
                    cout << "Correct answer: "
                    << addFirstNum + addSecondNum << " Sorry!\n";
                }
                cout << endl << endl;
                break;
            case 2:
                cout << "Working with subtraction\n";
                cout << setw(3) << subSecondNum << "\n"
                << "-" << multFirstNum << "\n"
                << "---\n";
                cout << "Your answer: ";
                cin  >> num;
                if(num == subSecondNum - multFirstNum)
                {
                    cout << "Correct answer: "
                    << subSecondNum - multFirstNum << " Congratulations!\n";
                }
                else
                {
                    cout << "Correct answer: "
                    << subSecondNum + multFirstNum << " Sorry!\n";
                }
                cout << endl << endl;
                break;
            case 3:
                cout << "Working with multiplication\n";
                cout << setw(3) << multFirstNum << "\n"
                << "*" << multSecondNum << "\n"
                << "---\n";
                cout << "Your answer: ";
                cin  >> num;
                if(num == multFirstNum * multSecondNum)
                {
                    cout << "Correct answer: "
                    << multFirstNum * multSecondNum << " Congratulations!\n";
                }
                else
                {
                    cout << "Correct answer: "
                    << multFirstNum + multSecondNum << " Sorry!\n";
                }
                cout << endl << endl;
                break;
            case 4:
                cout << "Thank you for using Math Tutor.\n";
                temBool =  false;

                break;
            default:  cout << "Incorrect menu seletion. Please choose between 1 and 4.\n"; 
                return 0;
        }
    }
}

2 个答案:

答案 0 :(得分:4)

简单:

在循环内移动随机值生成器行:

seed = time(0);
srand(seed);    //setting the seed should only be done outside the loop (as per original code)

while (temBool) //Start of the loop
{

    //Random generated operands
    addFirstNum = rand() % 40 + 10;
    addSecondNum = rand() % 40 + 10;
    subFirstNum = rand() % 40 + 10;
    subSecondNum = rand() % 40 + 10;
    multFirstNum = rand() % 40 + 10;
    multSecondNum = rand() % 40 + 10;

    //...
}

现在每次循环时,数字都会有一个新值。

编辑:答案现在在循环外显示srand(seed),以防您的问题是将该代码移动到循环中。
另外,就像whozcraig所说的那样,每次只需要两个随机数,因为你只使用每次生成的两个数字。

答案 1 :(得分:0)

只是对Baldrick的补充答案,当你用时间(0)播种然后只做一个循环,没有任何停止或询问用户下一步做什么(退出/继续)时,值将与你的种子相同具有相同的时间值(时间值没有变化,因为while循环快速处理了程序)。也许这不是问题,但是很高兴知道:)