|On this page…|
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 sysid, for example) that represents the system name, subsystem path name, or block path name.
Use the se_getseeds function with sysid as the input argument. The output is a structure having these fields:
system — Value of the sysid input to se_getseeds
seeds — Structure array, of which each element has these fields:
block — Path name of a block that uses a random number generator, relative to system
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 se_getseeds, see 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 se_setseeds function 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, use the se_randomizeseeds function.
For examples, see the Managing Seeds For Random Number GenerationManaging Seeds For Random Number Generation example. To learn about specific options for using se_randomizeseeds, see its reference page.
If your model uses random numbers in contexts other than SimEvents blocks, use the set_param function 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 se_getseeds and se_setseeds functions 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 procedure:
Create string variables (for example, sys1 and sys2) that represent the system names of the two models.
Open both models, if you have not already done so.
Use the se_getseeds function with sys1 as the input argument. The result is a seed structure that represents the seeds in the SimEvents blocks in model sys1.
Use the se_setseeds function with the seed structure as the first input argument and sys2 as 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 sys2 to values from model sys1.
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 get_param and set_param commands to retrieve and set the seeds, respectively. These examples illustrate the techniques:
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]
The term 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 seed represents 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
Query the seed parameter for its value.
thisseed = get_param(blk,'seed') thisseed = 60790
Change the value of the seed parameter 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 seed parameter 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 set_param once 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 se_randomizeseeds function or any other means.
Set the value of the seed to a varying expression such as 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.