Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MatrixXf::Random always returning same matrices

Tags:

c++

matrix

eigen

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;
}
like image 479
wlfbck Avatar asked Jan 22 '14 20:01

wlfbck


People also ask

How does the random number function work in MATLAB?

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.

Does Rand return the same result in MATLAB?

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.

How do I avoid repeating a random number array in MATLAB?

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.

Does random random range within a for loop return the same value?

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.


Video Answer


1 Answers

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));
like image 134
ggael Avatar answered Oct 13 '22 01:10

ggael