Search code examples
c++eigen

Set random seed in Eigen


I am currently filling a vector with random numbers using Eigen::VectorXd::Random(n).

Is it possible to seed Eigen's random number generator for deterministic results? Or alternatively, pass a random number generator to Eigen? I found no mention of either in the Eigen wiki, but it seems odd that such a feature would be missing.


Solution

  • It calls rand() from the standard library. You can set the seed by calling srand().

    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));