随机数组选择器

Ale*_*ost 5 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;
} 
Run Code Online (Sandbox Code Playgroud)

Mar*_*arc 5

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

你可以这样做:

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()
Run Code Online (Sandbox Code Playgroud)

您可以在cppreference uniform_int_distribution页面上找到更大的代码段和更多详细信息

  • 请注意,它不适用于MinGW,因为它们的`random_device`总是产生相同的序列. (3认同)