Why does my compiled RAND function give the same values every time I run my MATLAB-generated standalone application?

337 views (last 30 days)
I tried to compile the code below to an executable:
function myrand
When I run the executable, the RAND function always returns the same set of numbers. However, if I run the code in MATLAB, the function gives different results when I call it repeatedly.
The same thing happens if I generate the executable with the Matlab Coder.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
The RAND function in stand-alone applications generates the same numbers each time you run your application because the uniform random number generator that RAND uses is initialized to same state when the application is loaded.
You will notice that each time you start up a new MATLAB session, the random numbers returned by RAND are the same. This is because MATLAB's random number generator is initialized to the same state each time MATLAB starts up.
If you wish to generate different random values in each MATLAB session, you can use the system clock to initialize the random number generator once at the beginning of each MATLAB session.
In versions of MATLAB beginning with R2008b, the simplest way to do this is to execute the following command at the beginning of each MATLAB session:
For versions of MATLAB prior to R2008b you can execute the following commands:
In either case, it is only necessary to do this once at the beginning of each MATLAB session.
Similarly, you can execute the same command(s) in your standalone application. The code you compile might look like the following:
function myrand2
When you compile this to an executable, the state of the random number generator is initialized based on the system clock each time the application is run, and so the array "a" in the example above will contain a unique value each time.
Remember that it's only necessary to initialize the random number generator using the system clock once in your application.
For Matlab Coder, you have to use a slightly different approach as clock is not supported for codegen.
For example, consider
function myrand2
rng(sd,'twister');a=rand(4,1); disp(a)
For more information about MATLAB's random number generator, you can check the documentation by executing the following command at the MATLAB command prompt:
doc RandStream
help RandStream

More Answers (2)

Greg on 24 Oct 2018
In R2013a, "getDefaultStream" was replaced by "getGlobalStream" for the RandStream. Prior to that, it generated warnings indicating its eventual removal, but it began erroring in R2013a. For releases R2013a - R2018b (possibly later, but R2018b is the latest at time of writing), use:
For releases prior to R2013a, see the other answer.

Laurie König
Laurie König on 26 Aug 2021
Hi everyone, I do have a related question: I inherited some data and I can only see the result files. One number depends on randn. However it is different across participants ( I am sure that matlab was restarted for every participant). When I run the code on my local machine I get the same numbers as described above.
The code on my inherited data was ran on a Linux system where license was obtained from a server. Does the configuration of random number depends on whether you are on a standalone licese or obtain a license from a server? Or does it depend on your matlab version or which operating system you are on ( I am now on Windows and data used to be acquired on Linux). I would be happy for every help!

Community Treasure Hunt

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

Start Hunting!

Translated by