%% Create Independent Streams to Simulate Random Walk
% Create three mutually independent streams to simulate one-dimensional random walks using the method RandStream.create.

[s1,s2,s3] = RandStream.create('mrg32k3a','NumStreams',3);

% Generate a random walk from the first stream. First, set it as the global stream. Use the first stream to generate 5,000 random steps from the standard normal distribution. Choose the starting position at 0 and use cumsum to calculate the cumulative sum of the random steps. Plot the resulting random walk.

RandStream.setGlobalStream(s1)
dy1 = randn(5000,1);
y1 = cumsum([0; dy1]);
plot(y1)

%
% Repeat the process using the second and third streams. Plot the results on the same axes.

hold on;
dy2 = randn(s2,5000,1);
y2 = cumsum([0; dy2]);
plot(y2)
dy3 = randn(s3,5000,1);
y3 = cumsum([0; dy3]);
plot(y3)
hold off

% Calculate the correlations among the streams. The correlations between different streams are not exactly 0 because they are calculated from a sampling of the distribution.

C = corrcoef([dy1 dy2 dy3])

%
% Copyright 2020 The MathWorks, Inc.