bab2min / EigenRand

Fastest Random Distribution Generator for Eigen
https://bab2min.github.io/eigenrand/
MIT License
92 stars 13 forks source link

error C2466: cannot allocate an array of constant size 0 #39

Closed JohnPekl closed 2 years ago

JohnPekl commented 2 years ago

I want to random matrix using C++11 RNG such as std::mt19937 with std::random_device. Every time it randoms different numbers. Your example using Rand::P8_mt19937_64 urng{ 42 }; produces the same number if I run in a loop.

The following code is working fine (building without any errors) with matrix, vector size is 4. It can also run and return a random matrix.

        Eigen::Vector<double, 4> mu1;
        Eigen::Matrix<double, 4, 4> cov1;
        Eigen::Matrix<double, 4, -1> samples;
        Eigen::Rand::MvNormalGen<double, 4> gen_init{ mu1, cov1 };
        std::random_device rd;
        std::mt19937 genn(rd());
        samples = gen_init.generate(genn, 10);

But if I change the size to 2, 3, or 9 it has an error. eigenrand\Dists/Basic.h(260): error C2466: cannot allocate an array of constant size 0

        Eigen::Vector<double, 3> mu1;
        Eigen::Matrix<double, 3, 3> cov1;
        Eigen::Matrix<double, 3, -1> samples;
        Eigen::Rand::MvNormalGen<double, 3> gen_init{ mu1, cov1 };
        std::random_device rd;
        std::mt19937 genn(rd());
        samples = gen_init.generate(genn, 10);

NOTE: It happens on Windows, Ubuntu has no problem

bab2min commented 2 years ago

Hi @JohnPekl, thank you for reporting the bug. I found that double type random generators with 32bit rng(e.g. std::mt19937) throws the bug. You can avoid this bug by using 64bit rng such as std::mt19937_64 instead of std::mt19937.

I'll fix this bug at the next update. Thank you again for your interest!