Search code examples
c++matrixeigen

MatrixXf::Random always returning same matrices


I just played around with Eigen a bit and noticed that MatrixXf::Random(3,3) always returns the same matrices, first one is always this for example:
0.680375 0.59688 -0.329554
-0.211234 0.823295 0.536459
0.566198 -0.604897 -0.444451

Is that intended behaviour or am i just overseeing something really simple? (My experience with mathematic libraries is close to zero)

Code i used:

for(int i = 0; i < 5; i++) {
        MatrixXf A = MatrixXf::Random(3, 3);
        cout << A <<endl;
}

Solution

  • Yes, that's the intended behaviour. Matrix::Random uses the random number generator of the standard library, so you can initialize the random number sequence with srand(unsigned int seed), for instance:

    srand((unsigned int) time(0));