rand()不工作,给我一个超出指定最大值和最小值的数字

0 c++ random visual-c++ c++11

所以我正在制作一个游戏,你是老板和招聘员工,我以前用过rand,但由于某种原因它现在不能正常工作:

生成随机数的代码:

class employee
{

public:

int getSkills(int askingPrice)

    {
        srand((unsigned)time(0));

        switch(askingPrice)
        {
        case 1: //point of this is so if askingPrice is from 1 to 5 it will execute the same code
        case 2:
        case 3:
        case 4:
        case 5:

            skills = rand() % 5 + 1;

        }
        return skills;
    }
    int returnSkills()
    {
        return skills;
    }

    int getPaid(int askingPrice)
    {
        srand((unsigned)time(0));
        switch(askingPrice)
        {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:


            paid = rand() % 5 + 1;

        }
        return paid;
    }
    int returnPaid()
    {
        return paid;
    }

    int getHappy(int askingPrice)
    {
        srand((unsigned)time(0));
        switch(askingPrice)
        {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:


            happy = rand() % 5 + 1;

        }
        return happy;
    }
    int returnHappy()
    {
        return happy;
    }
private:
    int skills, paid, happy;
};
Run Code Online (Sandbox Code Playgroud)

显示随机数的代码:

std::cout <<"Your first employee is: " << one.returnPaid() << " salary, " <<             one.returnSkills() << " skilled @ his job, " << one.returnHappy() << " happy with his job";
Run Code Online (Sandbox Code Playgroud)

由于某种原因,它显示如下:

Your first employee is: -858993460 salary, 3 killed @ his job, 3 happy with his job
Run Code Online (Sandbox Code Playgroud)

为什么我得到薪水的负数而不是其他什么?

我正在使用Visual Studio 2010

Osw*_*ald 5

当你调用时one.returnPaid(),one.paid尚未初始化,因为one.getPaid()(唯一one.paid将接收值的函数)尚未被调用.