samedi 15 avril 2023

How to properly generate multiple random values using random in c++?

I am writing a program that selects a random string from a 2D vector in C++. This involves generating 2 random values; the first to select a subvector, and the second to select a string from that subvector.

std::string Mountains::getRandomMountain()
{
    int randomValue;
    string randomMountain;

    std::vector<std::string>currentRange;

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist1(0, rangeNames.size() - 1);

    randomValue = dist1(gen);

    correctRange = rangeNames[randomValue];

    currentRange = allMountains[randomValue];


    std::uniform_int_distribution<int> dist2(0, currentRange.size() - 1);
    randomValue = dist2(gen);


    randomMountain = currentRange[randomValue];


    return randomMountain;

}

This version of the code functions correctly when I debug it in Visual Studio. Still, when I try to Debug Test I am presented with a Debug Assertion Failed error: invalid min and max arguments for uniform_int. This only occurs the second time I try to access uniform_int_distribution, not the first. Any help would be much appreciated.




Aucun commentaire:

Enregistrer un commentaire