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

8 Ansichten (letzte 30 Tage)
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)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentar
Lex
Lex am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Random Number Generation finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by