mercredi 9 septembre 2015

Inclusive range uniform_real_distribution

I'm trying to use the random library. I know there's a similar question to this here: std::uniform_real_distribution inclusive range. From what I've read it should be [0, 10). I try to have [0, 10] but I've tried the solution and it doesn't work for me. I can't figure out why. Here's a bit of code.

std::vector<int> vec;

int main()
{   
    const int min = 0;
    const int max = 10;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(min, std::nextafter(max, INT_MAX));
    const int MAX = 10; 
    for (int i = 0; i < MAX; i++)
    {
        int t = dis(gen);
        vec.push_back(t);
    }
    for (auto& i : vec)
        std::cout << i << std::endl;
    system("pause");
    return 0;
}

I've tried:

std::uniform_real_distribution<> dis(min, std::nextafter(max, INT_MAX));
std::uniform_real_distribution<> dis{ 0, 10 };
std::uniform_real_distribution<> dis(min, max);

It only produces 0 through 9 as random and doesn't include 10 like I want it to. I'm working on VS2013.




Aucun commentaire:

Enregistrer un commentaire