MATLAB Answers

0

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

Asked by MathWorks Support Team on 18 Oct 2013
Latest activity Answered by Greg
on 24 Oct 2018
I tried to compile the code below to an executable:
function myrand
a=rand(4,1);
disp(a)
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.

2 Answers

Answer by MathWorks Support Team on 18 Oct 2013
 Accepted Answer

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:
reset(RandStream.getDefaultStream,sum(100*clock))
For versions of MATLAB prior to R2008b you can execute the following commands:
rand('twister',sum(100*clock))
randn('state',sum(100*clock))
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
reset(RandStream.getDefaultStream,sum(100*clock))
a=rand(4,1);
disp(a)
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
coder.cinclude('"time.h"')
sd=0;
sd=coder.ceval('time',[]);
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
%or
help RandStream

  2 Comments

Sign in to comment.


Answer by 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:
reset(RandStream.getGlobalStream,sum(100*clock));
For releases prior to R2013a, see the other answer.

  0 Comments

Sign in to comment.