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;
}
All the random number functions, rand, randn, randi, and randperm, draw values from a shared random number generator. Every time you start MATLAB ®, the generator resets itself to the same state. Therefore, a command such as rand(2,2) returns the same result any time you execute it immediately following startup.
Also, any script or function that calls the random number functions returns the same result whenever you restart. before calling rand, randn, randi , or randperm. This command ensures that you do not repeat a result from a previous MATLAB session.
If you want to avoid repeating the same random number arrays when MATLAB restarts, then execute the command, rng('shuffle'); before calling rand, randn, randi, or randperm. This command ensures that you do not repeat a result from a previous MATLAB session.
Random.range within a for loop returns same value everytime. I have this code, and everytime I run it, it display the same value. It is always 0, 6, 3, 1.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With