Why does my compiled RAND function give the same values every time I run my MATLAB-generated standalone application?
164 ビュー (過去 30 日間)
I tried to compile the code below to an executable:
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.
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:
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
For more information about MATLAB's random number generator, you can check the documentation by executing the following command at the MATLAB command prompt:
その他の回答 (2 件)
Greg 2018 年 10 月 24 日
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 2021 年 8 月 26 日
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!