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

8 visualizaciones (últimos 30 días)
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)

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Ag. de 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 comentario
Lex
Lex el 2 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by