Varying Simulation Results by Managing Seeds

See also Detecting Nonunique Seeds and Making Them Unique.

Connection Between Random Numbers and Seeds

When a simulation uses random numbers and you compute statistical results from it, you typically want to use different sequences of random numbers in the random processes of a single simulation run and 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:

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.

Making Results Repeatable by Storing Sets of Seeds

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 Setting Seed Values Programmatically to learn more.

When all seeds are parameters of SimEvents blocks, use this procedure to store the seeds:

  1. Decide whether you want to store seeds from SimEvents blocks in a system (including subsystems at any depth) or from a single block.

  2. Create a string variable (called sysid, for example) that represents the system name, subsystem path name, or block path name.

  3. Use the se_getseeds function with sysid as the input argument. The output is a structure having these fields:

  4. 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 Generators demo.

If your model uses random numbers in contexts other than SimEvents blocks, see Working with Seeds Not in SimEvents® Blocks.

Setting Seed Values Programmatically

To set seed values programmatically in blocks that use random numbers, use one or more of these approaches:

Sharing Seeds Among Models

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:

  1. Create string variables (for example, sys1 and sys2) that represent the system names of the two models.

  2. Open both models, if you have not already done so.

  3. 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.

  4. 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.

Working with Seeds Not in SimEvents® Blocks

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:

Example: Retrieving and Changing a Seed in a Custom Subsystem

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 demo model.

  1. Open the demo model.

    sedemo_md1
  2. 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
  3. 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
  4. Query the seed parameter for its value.

    thisseed = get_param(blk,'seed')
    
    thisseed =
    
    60790
  5. Change the value of the seed parameter to a constant.

    newseed = '60791'; % String whose value is a number
    set_param(blk,'seed',newseed);

    See Choosing Seed Values for criteria related to the values you choose for seeds.

  6. 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

Example: Retrieving and Changing the Seed for Event Randomization

This example illustrates how to query and change the Seed for event randomization configuration parameter programmatically.

  1. Open the demo model.

    sys = 'sedemo_event_priorities';
    open_system(sys);
  2. Retrieve the value of the Seed for event randomization configuration parameter.

    thiseventseed = get_param(sys,'propIdentEventSeed')
    
    thiseventseed =
    
    12345
  3. Change the value to a constant.

    neweventseed = '82937'; % String whose value is a number
    set_param(sys,'propIdentEventSeed',neweventseed);
  4. Change the value 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.

    eventseedvariable = 82938; % Numeric variable
    set_param(sys,'propIdentEventSeed,...
       'seedvariable'); % Parameter refers to variable

Choosing Seed Values

Here are some recommendations for choosing appropriate values for seed parameters of blocks:

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS