When a simulation uses random numbers and you compute statistical results from it, you typically want to use different sequences of random numbers in these situations:
In the random processes of a single simulation run
Across multiple simulation runs
To vary a sequence of random numbers, vary the initial seed on which the sequence of random numbers is based. SimEvents® blocks that have a parameter called Initial seed include:
Time-Based Entity Generator
Event-Based Random Number
Blocks in the Routing library
Some blocks in other library sets have parameters that represent initial seeds. For example, the Random Number and Uniform Random Number blocks in the Simulink® Sources library have parameters called Initial seed.
Also, if your simulation is configured to randomize the sequence of certain simultaneous events, the Configuration Parameters dialog box has a parameter called Seed for event randomization. This parameter indicates the initial seed for the sequence of random numbers that affect processing of simultaneous events.
If you need to repeat the results of a simulation run and expect to change random number sequences, then you should store the seeds before changing them. You can later repeat the simulation results by resetting the stored seeds; see Set Seed Values Programmatically to learn more.
When all seeds are parameters of SimEvents blocks, use this procedure to store the seeds:
Decide whether you want to store seeds from SimEvents blocks in a system (including subsystems at any depth) or from a single block.
Create a string variable (called
for example) that represents the system name, subsystem path name,
or block path name.
To avoid typing names, use
sysid as the input argument. The output is
a structure having these fields:
system — Value of the
seeds — Structure array,
of which each element has these fields:
block — Path name of a block
that uses a random number generator, relative to
value — Numeric seed value
of the block
Store the output in an array, cell array, or MAT-file. Use a MAT-file if you might need to recover the values in a different session.
For an example that uses
the Seed Management
Workflow for Random Number GeneratorsSeed Management
Workflow for Random Number Generators example.
If your model uses random numbers in contexts other than SimEvents blocks, see Work with Seeds Not in SimEvents Blocks.
To set seed values programmatically in blocks that use random numbers, use one or more of these approaches:
If you have a seed structure in the same format as
the output of the
se_getseeds function, use the
to set seed values in the corresponding blocks.
For an example, see the Seed Management Workflow for Random Number GeneratorsSeed Management Workflow for Random Number Generators example.
If you want the application to choose seed values
for you and then set the values in some or all SimEvents blocks,
For examples, see the Managing Seeds For
Random Number GenerationManaging Seeds For
Random Number Generation example. To learn about specific
options for using
see its reference page.
If your model uses random numbers in contexts other
than SimEvents blocks, use the
to set seed values.
For examples, see Work with Seeds Not in SimEvents Blocks.
Suppose you want to share seeds among multiple variants of a
model or among models that have a common subsystem. The
provide a convenient way to apply seed values of the SimEvents blocks
in one model to the corresponding blocks in a second model. Use this
Create string variables (for example,
that represent the system names of the two models.
Open both models, if you have not already done so.
sys1 as the input argument. The result is
a seed structure that represents the seeds in the SimEvents blocks
with the seed structure as the first input argument and
the second input argument. The function uses information from the
seed structure but overrides the system name stored in the seed structure.
As a result, the function sets the seeds in model
values from model
The seed management features in SimEvents software cover
blocks in the SimEvents libraries. If your model uses random
number sequences in other blocks or in the Seed for event
randomization configuration parameter, you can use
to retrieve and set the seeds, respectively. These examples illustrate
This example illustrates how to identify relevant variable names for seed parameters, query seed values, and set seed values. The specific block in this example is the Uniform Random Number block within a custom masked subsystem in a example model.
Open the example model.
Select the block labeled Exponential Generation and store its path name. Exponential Generation is a custom masked subsystem that has a seed parameter related to a Uniform Random Number block under the mask.
blk = gcb; % Pathname of current block
Query the dialog parameters of the block.
vars = get_param(blk,'DialogParameters') vars = seed: [1x1 struct]
seed in the output indicates a parameter's
underlying variable name, which can differ from the text label you
see in the block dialog box. You might guess that
the seed of a random number generator. Optionally, you can confirm
that this variable name corresponds to the Initial seed text
label in the dialog box using this command:
textlabel = vars.seed.Prompt textlabel = Initial seed
seed parameter for its
thisseed = get_param(blk,'seed') thisseed = 60790
Change the value of the
to a constant.
newseed = '60791'; % String whose value is a number set_param(blk,'seed',newseed);
See Choose Seed Values for criteria related to the values you choose for seeds.
Change the value of the
to the name of a variable in the workspace. As a result, the dialog
box shows the name of the variable instead of the value stored in
the variable. This approach might be useful if you want to use
and then change the workspace variable repeatedly (for example, within
a loop) to vary the seed value.
seedvariable = 60792; % Numeric variable set_param(blk,'seed',... 'seedvariable'); % Parameter refers to variable
Here are some recommendations for choosing appropriate values for seed parameters of blocks:
If you choose a seed value yourself, choose an integer between 0 and 231–1.
To obtain the same sequence of random numbers the next time you run the same simulation, set the seed to a fixed value.
To obtain a different sequence of random numbers the next time you run the same simulation, use one of these approaches:
Change the value of the seed, using the
or any other means.
Set the value of the seed to a varying expression
mod(ceil(cputime*99999),2^31). See the
cputime function for more details.
If seed parameters appear in multiple places in your model, choose different values, or expressions that evaluate to different values, for all seed parameters. To have the application detect nonunique seeds in SimEvents blocks, use the Identical seeds for random number generators configuration parameter. To learn how to make seeds unique in SimEvents blocks across a model, see Detect Nonunique Seeds and Making Them Unique.