MATLAB: Strange outcome matlab-intern and command-line

command linerandom number generatorsame result

Hi guys, something is bothering me and I can't get any clue how this problem can be solved: In my_m_file.m I generate a matrix of random numbers and save it:
N_max=200; M_max=16; alpha=(-1).^randi(2,[N_max,M_max]); save('alphafile.mat','alpha')
So when I run this code from inside Matlab, everything works fine and each time I run the code I get a different matrix. But, when I run this code from the (Ubuntu) command line, e.g. by
> cat my_m_file.m | matlab -nodesktop -nosplash
no errors occur, but I always get the same matrix! I tried this really couple of times, and very same result each run. I also wrote a script to run the code, as
#!/bin/bash ./test.sh matlab -nodisplay -nojvm -nosplash -nodesktop -r \ "try, run('<path_to_file>/my_m_file.m'), catch, exit(1), end, exit(0);"
but again: same result! I deleted the file "alphafile.mat" after each run to exclude the possibility that it could not have been overwritten. Please help me, what is the reason for this to happen, and even more important: how can this be solved??? Thanks a lot in advance!

Best Answer

When MATLAB starts, the random number generator seed is the same ( rng('default') ). To reseed it to something random, put this at the beginning:
rng shuffle