I\'m using Eigen and I\'ve got a matrix:
MatrixXi x = MatrixXi::Random(5);
I\'d like to randomly permute the rows and columns using a rando
As stated here: Stackoverflow:
If you can use C++11 I'd recommend implementing this without using
srand()andrandom_shuffle(); instead you should use thelibrary withstd::shuffle.First, if possible
randshould be avoided. Aside from the fact that it isn't usually a very good pRNG, it also has problems with thread safety due to shared state. Thelibrary fixes both these problems by giving the programmer explicit control over pRNG state and by providing several options with guaranteed performance, size, and quality characteristics.Secondly,
random_shuffleisn't actually specified to userandso it's theoretically legal for reseeding usingsrandnot to have the effect you want. To get guaranteed results withrandom_shuffleyou have to write your own generator. Moving toshufflefixes that, as you can directly use standard engines.
#include //seed generation
#include //shuffle()
int main() {
std::random_device r;
std::seed_seq rng_seed{r(), r(), r(), r(), r(), r(), r(), r()};
//create random engines with the rng seed
std::mt19937 eng1(rng_seed);
//create permutation Matrix with the size of the columns
Eigen::PermutationMatrix permX(inputX.cols());
permX.setIdentity();
std::shuffle(permX.indices().data(), permX.indices().data()+permX.indices().size(), eng1);
inputX = inputX * permX; //shuffle column wise
}
If you want to shuffle the rows use inputX.rows() instead for the initialization of the Permutation Matrix. And use inputX = permX * inputX instead.