Using rng: Why does changing the seed still give the same random sequence?

3 views (last 30 days)
Dear reader
When I run the following simple script the output displayed in the Matlab prompt shows two times the same random sequence while the seed has clearly changed. I'm wondering how this is possible? I know that by replacing rng(state) with rng(15) the seed can also be changed. (I tried it and the sequences are different as expected). However I would like to know why changing the seed directly in the state and then writing the state back, doesn't have the same effect.
Kind regards
Lex
script:
close all
clear
clc
rng('default')
state=rng; rng()
randn(1,5)
state.Seed=15;
rng(state); rng()
randn(1,5)
Output:
ans =
Type: 'twister'
Seed: 0
State: [625x1 uint32]
ans =
0.5377 1.8339 -2.2588 0.8622 0.3188
ans =
Type: 'twister'
Seed: 15
State: [625x1 uint32]
ans =
0.5377 1.8339 -2.2588 0.8622 0.3188
Matlab version: R2011b (7.13.0.564) 64-bit (win64)

Accepted Answer

Walter Roberson
Walter Roberson on 1 Aug 2012
The obvious postulate would be that the random number generator uses the State without checking whether the Seed field has changed. Changing the Seed field is not a defined mechanism for changing the state (or State) of the random number generator. Using the defined mechanisms for changing the seed changes the State.
Modern random number generators use a lot more than 32 or 64 bits of state information.
  1 Comment
Lex
Lex on 2 Aug 2012
Your right, the seed only determines the starting state of the RandStream. Later only the state is used to determine the next random number. By changing the seed and not the state the same sequence will come up. It is only when the RandStream gets constructed or reset that the seed influences the state.
Thanks for clarifying.

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!