mercredi 24 janvier 2018

mt19937 returns same number even though it is seeded

I have simple random generator code that returns different numbers using cl and g++ compilers in Windows. Strangely, the same code in Qt doesn't return different numbers. The code is

#include <random>
#include <iostream>

class RandGen
{
public:
    RandGen() :  m_generator(m_seeder())
    {
    }

    std::random_device m_seeder;
    std::mt19937 m_generator;

    int getNum()
    {
        std::uniform_int_distribution<> dist(0,5);
        return dist(m_generator);
    }
};


int main()
{
    RandGen rg;

    for(int i(0); i < 5; ++i){
        std::cout << rg.getNum() << " ";
    }
    std::cout << std::endl;

    return 0;
}

In Visual Studio, the result is

enter image description here

With g++ via cygwin, the result is

enter image description here

Now running same code with Qt using Desktop Qt 5.9.1 MinGW 32bit, it returns same values.

enter image description here

The .pro file is

QT += core
QT -= gui
CONFIG += c++11
TARGET = Ports
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app

Aucun commentaire:

Enregistrer un commentaire