随机数组选择器

时间:2018-01-01 13:13:57

标签: c++

我正在尝试制作一个锻炼计划,每次开始时都会选择随机锻炼和肌肉群。选择了肌肉群,我遇到了麻烦。

我希望它选择三个数组中的一个但是现在dice_roll的值总是等于2.不确定我哪里出错了。 谢谢你的帮助。

(OBS!请原谅我丑陋的代码,它似乎没有发布,因此可能会受到伤害!)

int main() 
{
    int muscleGroup;
    string chest[2] = {"Benchpress 4x2", "Pushups 10x4"};
    string legs[2] = {"Squat 8x4", "Leg extension 10x3"};
    string back[2] = {"Pullup 3x8", "Rows 10x3"};
    mt19937 generator; 

    uniform_int_distribution<int> distribution(0, 2);
    int dice_roll = distribution(generator);
    if (dice_roll == 0) 
    {
        cout << "You are training: Chest" << endl;
        cout << "Your exercises are going to be written below!" << endl;
    } 
    else if (dice_roll == 1) 
    {
        cout << "You are training: Legs" << endl;
        cout << "Your exercises are going to be written below!" << endl;
    } 
    else if (dice_roll == 2) 
    {
        cout << "You are training: Back" << endl;
        cout << "Your exercises are going to be written below!" << endl;
    }         

    // cin >> test;
    return 0;
} 

1 个答案:

答案 0 :(得分:6)

您需要使用随机种子初始化您的生成器。

您可以使用以下方式执行此操作:

std::random_device rd;  //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()

您可以在cppreference uniform_int_distribution page

上找到更大的代码段和更多详细信息