From: Peter Perkins <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: randn repeating itself
Date: Fri, 29 Mar 2013 16:19:54 -0400
Organization: The MathWorks, Inc.
Lines: 92
Message-ID: <kj4t1a$rpn$>
References: <kj0jon$9ec$> <kj2v6h$om4$> <kj4ot1$eaj$>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: 1364588394 28471 (29 Mar 2013 20:19:54 GMT)
NNTP-Posting-Date: Fri, 29 Mar 2013 20:19:54 +0000 (UTC)
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130307 Thunderbird/17.0.4
In-Reply-To: <kj4ot1$eaj$>
Xref: comp.soft-sys.matlab:792290

You said

>> I also tried using "rng shuffle", but matlab throws an error because the
>> random number is initialized with the legacy generator. Any advice?

I think you are referring to this error message:

    >> rng('shuffle')
    Error using rng (line 109)
    The current random number generator is the legacy generator.
    This is because you have executed a command such as
    rand('state',0), which activates MATLAB's legacy random number
    behavior.  You may not use RNG to reseed the legacy
    random number generator.

    Use rng('default') to reinitialize the random number generator
    to its startup configuration, or call RNG using a specific
    generator type, such as rng(seed,'twister').

If that's really true, then there is a line in code that you are running 
that is executing something like rand('state',0). You may not have 
written that line, but it's in there somewhere. That's what you need to 
find. To the best of my knowledge, there is no such line in any MATLAB 
toolbox code.

On 3/29/2013 3:09 PM, Nicholas wrote:
> Peter Perkins <> wrote in message
> <kj2v6h$om4$>...
>> On 2013/03/28 1:17, Nicholas wrote:
>> > This seems like a seeding problem to me, but as far as I can tell randn
>> > is used properly within this context. Though the way its functioning
>> > seems that the seed is set back to 'default' with each iteration.
>> > I also tried using "rng shuffle", but matlab throws an error because
>> the
>> > random number is initialized with the legacy generator. Any advice?
>> That error from the rng function indicates that something in the code
>> that you have not shown us is doing something along the lines of
>>     rand('state',0)
>> or
>>     randn('seed',0)
>> or something similar. This is bad on several levels, the most obvious
>> of which is that it's causing your image noise to be entirely
>> predictable. You should find the line of code and remove it.
>> > The problem is that the noise is the exact same with each iteration.
>>  > BUT the perplexing thing is that if I use a breakpoint inside the loop
>>  > and 'continue' through it, the noise changes with each iteration
>>  > (unlike if I were to just let it run).
>> That seems hard to imagine, but perhaps you could add the line
>>     rng % no semicolon
>> at the very beginning of your loop to try to diagnose what's going
>> wrong. Before you run the loop, type "rng default". Then see when the
>> legacy generator is being activated.
> Thank you for the reply. I should start with two things. First, I make
> no calls to rng in the function (I tried 'shuffle', but took it out) and
> randn is used exactly how I show in my example code above. Second, the
> data type correction noted by the previous poster. The first line should
> read:
> im = im2double(imread('lena.tif'));
> At any rate, I developed a workaround (a sloppy one at that) where I
> create a bin of all the noise models that have been previously used, I
> then compare the current model against all the others to ensure that the
> current is unique to the bin list (if it's unique it is then added to
> the bin), if it's not I keep generating new models until I get a new one
> that is unique.
> Consequently, what I found was that the number of attempts it takes to
> find a unique image corresponded to the i-th iteration in the for loop.
> So, say we're at i = 20 in the loop, then the first 19 attempts at
> creating a new image fails because they were already created and added
> to the bin at i = 1:19. This seems to indicate that rng is somehow being
> set to 'default' at the beginning of each pass of the loop. However, as
> I said above, I make no calls to rng, and randn is utilized exactly as
> shown above.
> Also, I don't know if it matters, but this block of code is nested
> within another for loop. It seems odd, but would nesting change the
> functionality of rng/randn?