strange outcome matlab-intern and command-line

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!

 Réponse acceptée

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

3 commentaires

Brilliant, thank you so much for your prompt reply and this solution! I would never have taken sth like that into account. So one last question: the documentation says
rng('shuffle') seeds the random number generator based on the current time
- does this mean the time after matlab-startup or the computer-time?
Sean de Wolski
Sean de Wolski le 20 Mar 2017
Modifié(e) : Sean de Wolski le 20 Mar 2017
The operating system clock.
Thanks, the program runs fine now!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by