Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Restoring the state of random generator

Subject: Restoring the state of random generator

From: Elnaz

Date: 17 Jun, 2013 21:07:13

Message: 1 of 6

Hi all,

At the beginning of my code I set the state of random number generator to zero by rng(0).
In the code I am averaging a final result over thousands of trials. At each trial I produce random numbers like the following:

for trial = 1:5000,
....
     msg = randint(n,L);
     noise = randn(m*k);
....
end

Now, my program because of a hardware issue keeps crashing after days of running. I do save the result at the end of each trials but for example the code runs untill trial = 1500 and then it stops!
Instead of re-running my code from scratch I want to calculate the state of the random number generator at the end of trial=1500 and then run the code from that point on as if it is being running from trial =1 and no crash has happened.
Since I know how many random numbers I generate for each trial and I know the trial in which my code has crashed, I think, I should be able to set the state to the point when my code has crashed. Does anybody know how should I do that?

Thanks,
Elnaz

Subject: Restoring the state of random generator

From: dpb

Date: 17 Jun, 2013 21:17:23

Message: 2 of 6

On 6/17/2013 4:07 PM, Elnaz wrote:
...

> each trial and I know the trial in which my code has crashed, I think, I
> should be able to set the state to the point when my code has crashed.
> Does anybody know how should I do that?
...

doc rng

--

Subject: Restoring the state of random generator

From: Elnaz

Date: 17 Jun, 2013 21:32:13

Message: 3 of 6

dpb,

The code is already crashed and since I didn't predict this crash happening I haven't saved the state of rng. I can regenerate those random numbers before the new trial loop begins:
rng(0);

for i = 1:1500,
     msg = randint(n,L);
     noise = randn(m*k);
end

for trial = 1501:5000, %continue from the crashing point
....
     msg = randint(n,L);
     noise = randn(m*k);
....
end

I meant to ask for a better solution than above? Is there any?

Elnaz

Subject: Restoring the state of random generator

From: Steven_Lord

Date: 17 Jun, 2013 22:01:24

Message: 4 of 6



"Elnaz " <ebsadeghian@gmail.com> wrote in message
news:kpnv8s$l8v$1@newscl01ah.mathworks.com...
> dpb,
> The code is already crashed and since I didn't predict this crash
> happening I haven't saved the state of rng. I can regenerate those random
> numbers before the new trial loop begins:
> rng(0);
>
> for i = 1:1500, msg = randint(n,L);
> noise = randn(m*k);
> end
>
> for trial = 1501:5000, %continue from the crashing point
> ....
> msg = randint(n,L);
> noise = randn(m*k);
> ....
> end
>
> I meant to ask for a better solution than above? Is there any?

No. What I suspect you're hoping is something along the lines of x1 and x2
being the same in this code:

rng(0)
rand(1)
x1 = rand(1);
rng(1)
x2 = rand(1);

It is not necessarily easy to tell, given an initial state and the number of
random samples drawn, the final state of the random number generator without
repeating the generation process.

To avoid this in the future, call RNG each iteration through the loop (or
every few iterations) to _query_ the state (not to set it) and store the
retrieved state.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Restoring the state of random generator

From: Elnaz

Date: 22 Jun, 2013 18:54:15

Message: 5 of 6

Right.
So I did according to the above method:
rng(0);
for i = 1:2,
     msg = randint(n,L);
     noise = randn(m*k);
end

for trial = 3, %continue from the crashing point
....
     msg = randint(n,L);
     noise = randn(m*k);
....
end
BUT, to my surprise the output msg generated above at trial=3 differs from the msg at trial=3 if generated continuously from the beginning i.e.:
rng(0);
for i = 1:3,
     msg = randint(n,L);
     noise = randn(m*k);
end
In the lines "...." above I am using LDPC encoder and decoder. Does that change the state of rng? If so, is there any way to know how many random numbers are generated at each call of the decode/encode function?
I am guessing that the two lines of "randint" and "randn" are not the only random generations in my code.

Thanks,
Elnaz

Subject: Restoring the state of random generator

From: dpb

Date: 23 Jun, 2013 14:52:59

Message: 6 of 6

On 6/22/2013 1:54 PM, Elnaz wrote:
> Right. So I did according to the above method:
> rng(0);
> for i = 1:2, msg = randint(n,L);
> noise = randn(m*k);
> end
>
> for trial = 3, %continue from the crashing point
> .....
> msg = randint(n,L);
> noise = randn(m*k);
> .....
> end
> BUT, to my surprise the output msg generated above at trial=3 differs
> from the msg at trial=3 if generated continuously from the beginning i.e.:
> rng(0);
> for i = 1:3, msg = randint(n,L);
> noise = randn(m*k);
> end
> In the lines "...." above I am using LDPC encoder and decoder. Does that
> change the state of rng? If so, is there any way to know how many random
> numbers are generated at each call of the decode/encode function?
> I am guessing that the two lines of "randint" and "randn" are not the
> only random generations in my code.

Well, it certainly isn't shown where you "did according to the above
method" ("the method" being the saving of the state after each iteration
and then restoring that state before resuming).

Show your work in sufficient detail to see what you actually did.

NB also that you'll have to resume _at_the_same_precise_stage_ in the
simulation as that point at which the state was previously saved in
order that the sequence will continue as if uninterrupted.

The number and position of the calls to the prng shouldn't matter if
your sequence of calls is predictable/reproducible at the point of the
restart point. If, otoh, you do have some other logic branch or an
external event that can change the state of any logic branch, then all
bets are off.

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us