Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random array selector

Tags:

c++

i'm trying to make a workout program that selects a random workout and musclegroup everytime you start it. I have ran into trouble with selecting wich musclegroup it's picking.

I would like it to pick one of the three arrays but right now the value of dice_roll is always equal to 2. Not sure where I have gone wrong. Thanks for any help.

(OBS! Excuse my ugly code, it doesn't seem to be posting right so it may be scued!)

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;
} 
like image 295
Alexanderrost Avatar asked Jan 01 '18 13:01

Alexanderrost


1 Answers

You need to initialize your generator with a random seed.

You can do so using:

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()

You can find a larger code snippet and more details on the cppreference uniform_int_distribution page

like image 76
Marc Avatar answered Sep 19 '22 05:09

Marc