image thumbnail
from Control random numbers in Simulink by John
Control random numbers in Simulink matlab function and random number generator

seed_setup_v4.m
%This script sets up the seed model. Its purpose is to demonstrate a way to
%save the random seed so that all simulations can be replicated. -- jrw
%01/26/12
close all; clear all; clear mex

%set a random seed for the embedded matlab functions
%s = RandStream.create('mt19937ar','seed',sum(100*clock));
%s = RandStream.create('mt19937ar','seed',1);
%RandStream.setDefaultStream(s);
%defaultStream = RandStream.getDefaultStream;
%savedState = defaultStream.State;
%u1 = rand(1,5)
%defaultStream.State = savedState;
%u2 = rand(1,5)


runs = 5;
sim_time = 10;
update_rate = 1;
run_seed = 1:runs;
run_output = zeros(runs,round(sim_time/update_rate)+1);
run_output2 = run_output;
run_output3 = run_output;
for i = 1: runs
    r_seed = run_seed(i);
    s = RandStream.create('mt19937ar','seed',r_seed);
    RandStream.setDefaultStream(s);
    %defaultStream.State = savedState;
    sim('seed_test_v4')
    run_output(i,:) = source_output;
    run_output2(i,:) = embed_output;
    run_output3(i,:) = gam_embed_output;
end

    

Contact us at files@mathworks.com