The problem of using rng to generate random numbers.

조회 수: 8 (최근 30일)
X Du
X Du 2012년 4월 11일
I want to use rng to generate several random number and save the state of generating them within a loop. so at the next step, I can use this state to continue generating randome number. e.g.:
for i=1:10
if i==1
rng(1);
else
load state
end
a=rand;
b=rand;...
save state
end
Does anybody know how to programme it?
Thanks a lot
Issac
  댓글 수: 2
X Du
X Du 2012년 4월 12일
What I want to do is that using the sequential state to generate random numbers when given a fix seed and a generator method.e.g. given rng(1),generating some random number and then record the current state which will be used as the initial state of next step (continue generating random numbers in the next loop).
for i=1:10
if i==1
rng(1);
else
load the state
use the state as the initial state
end
a=rand;
b=rand;...
save the current state
end
At the end I need this state sequence to repeat my simulations and also ensure I can control the rand numbers of every step within the loop.
X Du
X Du 2012년 4월 12일
it equals
rng(1)
for i=1:10
a=rand;
b=rand;...
end
But what I need to do is tracting the random number at every loop. e.g. the simulation stops when i=5,I donot want to run it from i=1. if I have the saved state, then I can use it to continue.

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

답변 (4개)

Daniel Shub
Daniel Shub 2012년 4월 11일
Why not create a new "stream"
doc RandStream
  댓글 수: 1
X Du
X Du 2012년 4월 12일
Thanks,but I really want to control every random number at each step. even if there is bug or stop,I can use the saved state to continue my simulation. then any suggetions?

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


Wayne King
Wayne King 2012년 4월 11일
When you call rng, you can output that information and save it if you wish
for nn = 1:10
scurr = rng;
rngstate{nn} = scurr.State;
x = randn(1,1);
end
  댓글 수: 1
X Du
X Du 2012년 4월 12일
Thanks,in the case you gave,rngstate would be the same.but I want to use the sequential state not the same state.
What I want to do is that using the sequential state to generate random numbers when given a fix seed and a generator method.e.g. given rng(1),generating some random number and then record the current state which will be used as the initial state of next step (continue generating random numbers in the next loop).
for i=1:10
if i==1
rng(1);
else
load the state
use the state as the initial state
end
a=rand;
b=rand;...
save the current state
end
At the end I need this state sequence to repeat my simulations and also ensure I can control the rand numbers of every step within the loop.
Is it possible and any suggetions? thanks

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


Richard Willey
Richard Willey 2012년 4월 11일
It's hard to make a specific recommendation without known more about the use case.
What (specifically) are you trying to accomplish / control?
  댓글 수: 1
X Du
X Du 2012년 4월 12일
sorry for the confused question.What I want to do is that using the sequential state to generate random numbers when given a fix seed and a generator method.e.g. given rng(1),generating some random number and then record the current state which will be used as the initial state of next step (continue generating random numbers in the next loop).
for i=1:10
if i==1
rng(1);
else
load the state
use the state as the initial state
end
a=rand;
b=rand;...
save the current state
end
At the end I need this state sequence to repeat my simulations and also ensure I can control the rand numbers of every step within the loop.
any suggetion? thanks

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


Richard Willey
Richard Willey 2012년 4월 12일
From the sounds of things, the simplest thing to do would be to generate all of your random numbers OUTSIDE your loop.
Start by setting your seed and generate a vector with all the random numbers that you need for all your loops. Index into this as necessary.
  댓글 수: 1
X Du
X Du 2012년 4월 12일
Thanks,
You are right,I thought it before.I have to do it in this way if there is no any other better way.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by