Path: news.mathworks.com!not-for-mail
From: Peter Perkins <Peter.Remove.Perkins.This@mathworks.com>
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$1@newscl01ah.mathworks.com>
References: <kj0jon$9ec$1@newscl01ah.mathworks.com> <kj2v6h$om4$1@newscl01ah.mathworks.com> <kj4ot1$eaj$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: ah-pperkins.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: newscl01ah.mathworks.com 1364588394 28471 172.28.17.104 (29 Mar 2013 20:19:54 GMT)
X-Complaints-To: news@mathworks.com
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$1@newscl01ah.mathworks.com>
Xref: news.mathworks.com 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 <Peter.Remove.Perkins.This@mathworks.com> wrote in message
> <kj2v6h$om4$1@newscl01ah.mathworks.com>...
>> 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?