From: Matthew on
In Matlab, I am able to change the default stream to create a different sequence of random numbers. I would like to do the same thing in Simulink.

Lets say I have a simple Time-Based Entity Generator where I do not want to change the random seed. However, I would like to change the default stream for the random generator process so that I can realize different results when I run the simulation. Is it possible to change the default stream to accomplish this?

Additionally, I would like to use the control the rand() function in embedded Matlab code. Specifically, from one run of a simulation to the next, I would like to use a different random number seed to vary results of the simulation. Can I use the RandStream function in embedded Matlab code to set the random seed? If not, how do I set a new random seed on the rand() function in embedded Matlab code?
From: Devdatt Lad on
Matthew,

SimEvents random number generators do not support the changing of streams
because these generators don't use MATLAB's rand and RandStream utilities.
To generate different sequences of numbers, you will need to change the seed
on these blocks. Some utility functions that may make this easier for you
are: se_randomizeseeds, se_getseeds and se_setseeds. See the help on these
functions, as well as the two SimEvents demos on managing seeds in your
model.

Embedded MATLAB currently does not support RandStream, because it does not
support MATLAB objects in general. To work around this you can define a new
MATLAB function (.m file) that works with a persistent RandStream object,
and simply returns a numeric value. Declare this function extrinsic in EML
code. See the documentation of eml.extrinsic.

Hope this helps,

-Devdatt

----
Devdatt Lad
The MathWorks, Inc.


"Matthew " <matthew.durchholz(a)lmco.com> wrote in message
news:hrac51$gvj$1(a)fred.mathworks.com...
> In Matlab, I am able to change the default stream to create a different
> sequence of random numbers. I would like to do the same thing in Simulink.
>
> Lets say I have a simple Time-Based Entity Generator where I do not want
> to change the random seed. However, I would like to change the default
> stream for the random generator process so that I can realize different
> results when I run the simulation. Is it possible to change the default
> stream to accomplish this?
>
> Additionally, I would like to use the control the rand() function in
> embedded Matlab code. Specifically, from one run of a simulation to the
> next, I would like to use a different random number seed to vary results
> of the simulation. Can I use the RandStream function in embedded Matlab
> code to set the random seed? If not, how do I set a new random seed on the
> rand() function in embedded Matlab code?
>


From: Michael Hosea on
"Matthew " <matthew.durchholz(a)lmco.com> wrote in message
news:hrac51$gvj$1(a)fred.mathworks.com...
> Additionally, I would like to use the control the rand() function in
> embedded Matlab code. Specifically, from one run of a simulation to the
> next, I would like to use a different random number seed to vary results
> of the simulation. Can I use the RandStream function in embedded Matlab
> code to set the random seed? If not, how do I set a new random seed on the
> rand() function in embedded Matlab code?

Because classes are not yet supported in Embedded MATLAB, you must use the
"legacy mode" control of RAND and RANDN. To seed the generator, use (for
example)

rand('twister',1234)

where the second argument is your seed. You have several options for
managing that seed value. Probably you don't want to re-seed every time
step, so you might use a persistent variable. The following example
illustrates a couple of things. First, it shows managing the seeding of the
Embedded MATLAB RAND function using a persistent variable, so that seeding
only occurs in the first time step. Second, to obtain that seed it shows
how to call out to MATLAB's RANDI function, which I'm using to generate a
seed. This is kind of silly insofar as I could use the same technique to
call MATLAB's RAND function, but notice that I had to define the output
before I called RANDI. If you have various call sites that ask for
different size arrays from the extrinsic functions, you may find it annoying
to have to "declare" the output type like that before each call.

function y = foo %#eml
eml.extrinsic('randi');
persistent firstcall
if isempty(firstcall)
seed = 0;
seed = randi(intmax);
rand('twister',seed);
firstcall = false;
end
y = rand(1,3);

If you use an extrinsic function, however, you cannot generate code using
RTW. Some other options are to use a non-tunable parameter input for the
seed (the value is fetched from a variable in your workspace) or an argument
that is coming from a constant block in Simulink.
--
Mike