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:
rand function

Subject: rand function

From: Twilighter

Date: 20 Jun, 2012 17:22:05

Message: 1 of 11

Hi I need to test the rand fucntion

but i get this error

Error using ==> rand
State must be a double scalar or the output of RAND('state').

Error in ==> pn_test at 13
rand('state',Key);


this my Code what is wrong in there ?

% read in key for PN generator
file_name='_Key.jpg';
Orig_key=double(imread(file_name))./256;
Key = reshape(Orig_key,1,[]);

% reset MATLAB's PN generator to state "key"
rand('state',Key);

% execute rand 20 times
for i=1:20
    test1(i)=rand;
end

% reset MATLAB's PN generator to state "key"
rand('state',Key);

% generate 20 element random vector
test2=rand(1,20);

% check to make sure they're equal
difference=sum(test1-test2),

Subject: rand function

From: dpb

Date: 20 Jun, 2012 17:55:09

Message: 2 of 11

On 6/20/2012 12:22 PM, Twilighter wrote:
> Hi I need to test the rand fucntion
>
> but i get this error
>
> Error using ==> rand
> State must be a double scalar or the output of RAND('state').
>
> Error in ==> pn_test at 13
> rand('state',Key);
>
>
> this my Code what is wrong in there ?
>
> % read in key for PN generator
> file_name='_Key.jpg';
> Orig_key=double(imread(file_name))./256;
> Key = reshape(Orig_key,1,[]);
>
> % reset MATLAB's PN generator to state "key"
> rand('state',Key);
...

Well, it wouldn't appear that Key passed to rand() is a scaler given the
preceding code and unless the machinations performed match the
conditions of the output of a previous call to rand('state') then it
would seem pretty clear from the error message what you've tried to do
isn't legal...

And, what you're attempting is probably misguided at best, anyway...I
suggest a serious perusal of doc rand and the links therein,
particularly the reference at

<http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html#bsvcr7h>

--

Subject: rand function

From: Twilighter

Date: 20 Jun, 2012 18:16:07

Message: 3 of 11

I needed this generated random numbers as a watermark key, that i wanted it to be secret, and recoverable so in the detection part I can generate that same Watermark again ... i saw the documentation in which it uses the rand('state') as for the next used rand generation as such

s = rand('state')
rand('state', s)

but isn't there anyway that i can make this S my own generated sequence? or more generally i need to generate an encoding Key to watermark an Image ... is there some known techniques for doing that?

Subject: rand function

From: Steven_Lord

Date: 21 Jun, 2012 14:15:44

Message: 4 of 11



"Twilighter " <mscmatlab@gmail.com> wrote in message
news:jrt417$l3e$1@newscl01ah.mathworks.com...
> I needed this generated random numbers as a watermark key, that i wanted
> it to be secret, and recoverable so in the detection part I can generate
> that same Watermark again ... i saw the documentation in which it uses
> the rand('state') as for the next used rand generation as such
>
> s = rand('state')
> rand('state', s)
> but isn't there anyway that i can make this S my own generated sequence?

Theoretically yes. In practice I would STRONGLY discourage you from doing
that for two reasons. It can be extremely tricky (and algorithm-dependent)
to generate a state vector that's "right" for the specific generator you're
using, and the syntax you're using is not recommended anymore.

http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html

> or more generally i need to generate an encoding Key to watermark an Image
> ... is there some known techniques for doing that?

Generate an integer seed using your encoding key and some algorithm like a
hash function:

http://en.wikipedia.org/wiki/Hash_function

and use RNG with that integer seed value to control the initial state of the
random number generator.

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

Subject: rand function

From: Twilighter

Date: 21 Jun, 2012 15:21:07

Message: 5 of 11

thank you steve another simple question if you dont mind

if i do have a
X = [0 0 0 0 0 ; 0 1 1 0 1 ; 0 1 1 1 0];

and i want to replace all zeros by -1 how can i do that, i remember it's just a one line code, but i forgot how to make it

Subject: rand function

From: Twilighter

Date: 21 Jun, 2012 15:23:06

Message: 6 of 11

I also understood your reply but can you write me an example for it using hash function , if it's ok?

Subject: rand function

From: Twilighter

Date: 21 Jun, 2012 15:28:07

Message: 7 of 11

I got it

W(W==0) = -1;

I'm still looking for an example, too mayn threads here I know :)

Subject: rand function

From: dpb

Date: 21 Jun, 2012 18:06:45

Message: 8 of 11

On 6/21/2012 10:21 AM, Twilighter wrote:
> thank you steve another simple question if you dont mind
> if i do have a X = [0 0 0 0 0 ; 0 1 1 0 1 ; 0 1 1 1 0];
>
> and i want to replace all zeros by -1 how can i do that, i remember it's
> just a one line code, but i forgot how to make it

It's better to make a thread on a single topic rather than jump off into
something totally unrelated...this is so trivial I'll answer it here but
in future...

x(x==0)=-1;

Look up "logical addressing"

--

Subject: rand function

From: Twilighter

Date: 21 Jun, 2012 19:32:06

Message: 9 of 11

thank you dpq ... although as you can see I have recovered my question, just 2 seconds after putting it, sometimes working under pressure does make you a bit irrelevant from the too much stress ... and there is a lot of pressure work I'm under, I do appreciate your reply

you mean by logical addressing this?

b = [0 6 5 0 1 0];
PN=b(logical(b));

Subject: rand function

From: dpb

Date: 21 Jun, 2012 19:54:55

Message: 10 of 11

On 6/21/2012 2:32 PM, Twilighter wrote:
> thank you dpq ... although as you can see I have recovered my question,
> just 2 seconds after putting it, sometimes working under pressure does
> make you a bit irrelevant from the too much stress ... and there is a
> lot of pressure work I'm under, I do appreciate your reply
> you mean by logical addressing this?
>
> b = [0 6 5 0 1 0];
> PN=b(logical(b));

Not exactly, no...

I see the section is actually now name "logical subscripting" instead of
addressing...

<http://www.mathworks.com/help/techdoc/learn_matlab/f2-644.html#f2-15124>

--

Subject: rand function

From: Steven_Lord

Date: 22 Jun, 2012 13:52:37

Message: 11 of 11



"Twilighter " <mscmatlab@gmail.com> wrote in message
news:jrve8q$o94$1@newscl01ah.mathworks.com...
> I also understood your reply but can you write me an example for it using
> hash function , if it's ok?

One simple hash function that would have a lot of collisions would be:

    simpleHash = @(x) mod(sum(x), 2^16);

To use it:

    key = randi([0 2^16-1], 1, 10) % Generate a random key
    hashOfKey = simpleHash(key)

    % To show that the function is reproducible, let's try it again
    hashOfKey = simpleHash(key) % This should give the same result as above

    rng(hashOfKey)
    x = rand(1, 10)

Try this several times with different key vectors and you'll see different x
vectors if the hashOfKey is different.

You'll probably want to investigate more sophisticated hash functions for
your actual application.

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

Tags for 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