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

조회 수: 7 (최근 30일)
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)

채택된 답변

Walter Roberson
Walter Roberson 2012년 8월 1일
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
Lex
Lex 2012년 8월 2일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by