COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to set the seed as a 128 binary string of psudo random number generator (randn)

• Follow

```Hi All,

I'm doing a research, which requires to seed the pseudo random number generator with a 128 bit binary string (eg. 11001101) and obtain a vector of 1000 random numbers.

From what I have gathered, we can set the seed either as a scalar or as a N length vector. I'm successful in setting the seed as a scalar, but the challenge that I'm facing is that it only accepts scalars of 10 bits, but my binary string is 128 bits long.

Still I can set the seed to 128 bits binary string. However, it gives me the same output for totally different 128 bits seeds.

eg.

Test 1:
k=10100101111110001010010100000110110100110100111011101010000111010001110110110110111010101100001111001001011111101111001110011000;
randn('state', k);
w=randn(1,10);
disp(w);

Test 2:
k=10010100101010101100011001000000100101101000011010001110011001111001111001011101011111010011010100010011010110011000101001010010;
randn('state', k);
w=randn(1,10);
disp(w);

Both Test 1 and Test 2 return the same result:

Columns 1 through 9

0.7953    0.0222    0.9484    0.6454   -0.6079    1.6226   -0.6279    0.3315    0.0232

Column 10

-0.6825

Greatly appreciate if someone can help me with the problem I'm facing.

```
 0
Reply Randima 6/6/2010 2:00:05 PM

```Randima Hettiarachchi wrote:

> I'm doing a research, which requires to seed the pseudo random number
> generator with a 128 bit binary string (eg. 11001101) and obtain a
> vector of 1000 random numbers.

You will have to implement your own random number generator, as that is
not possible in Matlab.

Matlab does not provide any random stream which can be initialized with
128 bits. Matlab random number generators can be initialized with a 32
bit positive integer, or they can be initialized with an exact copy of
their internal state, where their internal state is a cell array which
varies with the generator but often has components that are more than
300 uint32.

> From what I have gathered, we can set the seed either as a scalar or as
> a N length vector.

If that was true once, it is no longer true, not in Matlab 2008a
onwards. Are you perhaps using an older Matlab?

> Still I can set the seed to 128 bits binary string. However, it gives me
> the same output for totally different 128 bits seeds.
> eg.
> Test 1:
> k=10100101111110001010010100000110110100110100111011101010000111010001110110110110111010101100001111001001011111101111001110011000;

After that, k would not be a binary bit stream: it would be a 64 bit
double precision number in the range 1.01001 * 10^128. There is no way
in Matlab to create a 128 bit integer data type (other than using the
Fixed Point Toolbox, but those datatypes are not known to the random
number generator.)

> randn('state', k);

'state' indicates the older "subtract with borrow" random number generator.

You should be transitioning to using RandStream.create rather than using
randn('state',k)
```
 0
Reply Walter 6/6/2010 2:49:51 PM

```Walter Roberson <roberson@hushmail.com> wrote in message <kAOOn.6681\$3y2.4867@newsfe11.iad>...
> Randima Hettiarachchi wrote:
>
> > I'm doing a research, which requires to seed the pseudo random number
> > generator with a 128 bit binary string (eg. 11001101) and obtain a
> > vector of 1000 random numbers.
>
> You will have to implement your own random number generator, as that is
> not possible in Matlab.
>
> Matlab does not provide any random stream which can be initialized with
> 128 bits. Matlab random number generators can be initialized with a 32
> bit positive integer, or they can be initialized with an exact copy of
> their internal state, where their internal state is a cell array which
> varies with the generator but often has components that are more than
> 300 uint32.
>
>
> > From what I have gathered, we can set the seed either as a scalar or as
> > a N length vector.
>
> If that was true once, it is no longer true, not in Matlab 2008a
> onwards. Are you perhaps using an older Matlab?
>
> > Still I can set the seed to 128 bits binary string. However, it gives me
> > the same output for totally different 128 bits seeds.
> > eg.
> > Test 1:
> > k=10100101111110001010010100000110110100110100111011101010000111010001110110110110111010101100001111001001011111101111001110011000;
>
> After that, k would not be a binary bit stream: it would be a 64 bit
> double precision number in the range 1.01001 * 10^128. There is no way
> in Matlab to create a 128 bit integer data type (other than using the
> Fixed Point Toolbox, but those datatypes are not known to the random
> number generator.)
>
>
> > randn('state', k);
>
> 'state' indicates the older "subtract with borrow" random number generator.
>
> You should be transitioning to using RandStream.create rather than using
> randn('state',k)

#################################################
Thanks a lot sir. This helps. I think the below one should work regarding setting the seed using RandStream.create.

s1 = RandStream.create('mrg32k3a','NumStreams',1);
RandStream.setDefaultStream(s1);
r1 = rand(s1,1000,k);

Sir, I'm using Matlab R2008a. Is RandStream available in that? It gives me an error, when I try to compile the code.
```
 0
Reply Randima 6/6/2010 3:34:04 PM

```Randima Hettiarachchi wrote:

> Sir, I'm using Matlab R2008a. Is RandStream available in that? It gives
> me an error, when I try to compile the code.

Sorry, my mistake, RandStream is 2008b onwards. Guess I've been using
2008b longer than I remembered.

> s1 = RandStream.create('mrg32k3a','NumStreams',1);
> RandStream.setDefaultStream(s1);
> r1 = rand(s1,1000,k);

Unfortunately even if you had 2008b onward, random number seeds must be
integers from 1 to 2^32-1, but your k as given earlier is a double
precision floating point number in the range 1E128 (about 2^384 or so)
```
 0
Reply Walter 6/6/2010 3:59:38 PM

```On 6/6/2010 11:34 AM, Randima Hettiarachchi wrote:
> I think the below one should work
> regarding setting the seed using RandStream.create.
>
> s1 = RandStream.create('mrg32k3a','NumStreams',1);
> RandStream.setDefaultStream(s1);
> r1 = rand(s1,1000,k);

A more straightforward version would be

s1 = RandStream('mrg32k3a');
RandStream.setDefaultStream(s1);
r1 = rand(1000,k);

however, you haven't specified a seed (it defaults to 0), which appears
to be the thing you are asking to do.  A seed can be passed as an
addition argument pair to RandStream, however, it still cannot be 128
bits.  And of course since you have MATLAB R2008a, the above will not
work for you.
```
 0
Reply Peter 6/7/2010 12:51:53 PM

```Randima, the generators in MATLAB accept an integer value between 0 and
2^31 as a seed, or they accept a state vector that has previously been
read from the generator.  You cannot expect to take an arbitrary 128
bits and use that as a state vector (and as Walter pointed out, you
weren't doing that anyway).

It appears as if you want, in effect, to use a 128bit value as a seed.
Perhaps if you explained your requirement for that, it would help.

On 6/6/2010 10:00 AM, Randima Hettiarachchi wrote:
> Hi All,
>
> I'm doing a research, which requires to seed the pseudo random number
> generator with a 128 bit binary string (eg. 11001101) and obtain a
> vector of 1000 random numbers.
>
>  From what I have gathered, we can set the seed either as a scalar or as
> a N length vector. I'm successful in setting the seed as a scalar, but
> the challenge that I'm facing is that it only accepts scalars of 10
> bits, but my binary string is 128 bits long.
> Still I can set the seed to 128 bits binary string. However, it gives me
> the same output for totally different 128 bits seeds.
> eg.
> Test 1:
> k=10100101111110001010010100000110110100110100111011101010000111010001110110110110111010101100001111001001011111101111001110011000;
>
> randn('state', k);
> w=randn(1,10); disp(w);
>
> Test 2:
> k=10010100101010101100011001000000100101101000011010001110011001111001111001011101011111010011010100010011010110011000101001010010;
>
> randn('state', k);
> w=randn(1,10); disp(w);
>
> Both Test 1 and Test 2 return the same result:
>
> Columns 1 through 9
>
> 0.7953 0.0222 0.9484 0.6454 -0.6079 1.6226 -0.6279 0.3315 0.0232
>
> Column 10
>
> -0.6825
>
> Greatly appreciate if someone can help me with the problem I'm facing.
>
> Thanks in advance....

```
 0
Reply Peter 6/7/2010 12:52:55 PM

```Peter Perkins wrote:
> Randima, the generators in MATLAB accept an integer value between 0 and
> 2^31 as a seed

Not 2^31:

The limits on seed can be tricky to find in the documentation -- they
are not documented in @RandStream or rand() or Legacy Mode :: Random
Numbers ( techdoc/math/brt5wsv.html ), but they are documented in
RandStream (without the @) and in RandStream.create.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randstream.randstream.html

Seed	Nonnegative scalar integer with which to initialize all streams.
Default is 0. Seed must be an integer less than 2^32

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randstream.create.html

Seed	Nonnegative scalar integer with which to initialize all streams.
Default is 0. Seeds must be an integer between 0 and 2^32.

That last documentation is not clear on the meaning of "between"; the
naive reading would suggest that 0 and 2^32 exactly are valid inputs,
but 2^32 exactly is excluded by the first document, which implicitly
allows 0 (especially as 0 is the default.)

I would recommend the documentation be touched up with respect to the
limits.
```
 0
Reply Walter 6/7/2010 1:27:23 PM

```Walter Roberson <roberson@hushmail.com> wrote in message <0t6Pn.28155\$yx.24733@newsfe13.iad>...
> Peter Perkins wrote:
> > Randima, the generators in MATLAB accept an integer value between 0 and
> > 2^31 as a seed
>
> Not 2^31:
>
> The limits on seed can be tricky to find in the documentation -- they
> are not documented in @RandStream or rand() or Legacy Mode :: Random
> Numbers ( techdoc/math/brt5wsv.html ), but they are documented in
> RandStream (without the @) and in RandStream.create.
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randstream.randstream.html
>
> Seed	Nonnegative scalar integer with which to initialize all streams.
> Default is 0. Seed must be an integer less than 2^32
>
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randstream.create.html
>
> Seed	Nonnegative scalar integer with which to initialize all streams.
> Default is 0. Seeds must be an integer between 0 and 2^32.
>
>
> That last documentation is not clear on the meaning of "between"; the
> naive reading would suggest that 0 and 2^32 exactly are valid inputs,
> but 2^32 exactly is excluded by the first document, which implicitly
> allows 0 (especially as 0 is the default.)
>
> I would recommend the documentation be touched up with respect to the
> limits.

Hi Peter,
Thanks a lot. This helps to understand the boundaries of the seed. I'm doing a research, which requires me to feed the hash (MD5 hash) of an image to a pseudo random number generator to create a watermark (1*1000 matrix). The output of MD5 algorithm would always be a byte array of 128 bits.

I guess I may need to come up with my own random number generator in order to achieve this purpose as Walter has suggested.

Many thanks for the help and guidance..... it is much appreciated.
```
 0
Reply Randima 6/7/2010 2:25:22 PM

```On Jun 6, 10:00 am, "Randima Hettiarachchi" <randi...@yahoo.com>
wrote:
> Hi All,
>
> I'm doing a research, which requires to seed the
> pseudo random number generator with a 128 bit
> binary string (eg. 11001101)

That doesn't make sense. Please explain why this
is required.

> and obtain a vector of 1000 random numbers.
>
> From what I have gathered, we can set the seed
> either as a scalar or as a N length vector.

an "integer" or a 35-dim vector

> I'm successful in setting the seed as a scalar, but
> the challenge that I'm facing is that it only accepts
> scalars of 10 bits, but my binary string is 128 bits
> long.
>
> Still I can set the seed to 128 bits binary string.
> However, it gives me the same output for totally
> different 128 bits seeds.
>
> eg.
>
> Test 1:
> >k=3D10100101111110001010010100000110110100110100111011101010000111010001=
11011-0110110111010101100001111001001011111101111001110011000;

k =3D 1010010111 1110001010 0101000001 1011010011 01001110
111010100001110100011101101101101110101011000011110...
01001011111101111001110011000
> randn('state', k);
> w=3Drandn(1,10);
> disp(w);
>
> Test 2:
> k=3D100101001010101011000110010000001001011010000110100011100110011110011=
> randn('state', k);
> w=3Drandn(1,10);
> disp(w);
>
> Both Test 1 and Test 2 return the same result:
>
>  Columns 1 through 9
>
>     0.7953    0.0222    0.9484    0.6454   -0.6079    1.6226   -0.6279   =
0.3315    0.0232
>
>   Column 10
>
>    -0.6825
>
> Greatly appreciate if someone can help me with the
> problem I'm facing.
>
> Thanks in advance....

There are 2^1492 possible states.
All are associated with a 35-dim state vector.
However, N =3D 2^32 =3D 4294967296 of them are integer
states.
Seeds for the integer states are integers in the
interval [0,N-1].

Seeding with values exceeding N-1 generate the
same random sequence as that generated using N-1,

Your result is caused by values of k that are
being interpreted as nonbinary integers that
exceed N-1.

clear, clc
format long g
N =3D 2^32-1;
K =3D (N-3:N+3)';
i =3D 0;
for J =3D K(1):K(end)
i =3D i+1;
rand('state',J)
out(i,1) =3D rand;
end
summary =3D [K out]

summary =3D

4294967292     0.65729010184722
4294967293     0.980015862432923
4294967294     0.430671970060266
4294967295     0.662733934182293

4294967296     0.662733934182293
4294967297     0.662733934182293
4294967298     0.662733934182293

Now if you are trying to directly access 2^128
unique states of the available 2^1492, it
cannot be done. Obviously indirect methods
have to be used.

I don't know the constraints on the components
of the 35-dim state vector. However, whatever
they are, you could generate 2^128 of them by
using 27 4-bit binary integers and 12 3-bit
binary integers obtained from one 128-bit
binary integers.

Again, I don't see the reason for this constraint.
Using one nonnegative integer seed will guarantee
that the next 2^1492 -1 states will be chosen
(pseudo) randomly.

Hope this helps.

Greg

```
 0
Reply Greg 6/7/2010 4:53:03 PM

```On 6/7/2010 9:27 AM, Walter Roberson wrote:
> Peter Perkins wrote:
>> Randima, the generators in MATLAB accept an integer value between 0
>> and 2^31 as a seed
>
> Not 2^31:

Walter, you're right.  I hit the wrong key.

> The limits on seed can be tricky to find in the documentation -- they
> are not documented in @RandStream or rand() or Legacy Mode :: Random
> Numbers ( techdoc/math/brt5wsv.html ), but they are documented in
> RandStream (without the @) and in RandStream.create.

I think the allowable values for a seed are documented on both of the
reference pages where a seed is accepted as an input to create a stream,
which is to say the constructor and the create method, though not on the
reference page for the reset method.  You're right that both "Seed must
be an integer less than 2^32" and "Seeds must be an integer between 0
and 2^32" are not precise enough.  I've made a note to fix all of that,
thanks for pointing them out.
```
 0
Reply Peter 6/7/2010 5:51:31 PM

```Peter Perkins wrote:

> I think the allowable values for a seed are documented on both of the
> reference pages where a seed is accepted as an input to create a stream,
> which is to say the constructor and the create method, though not on the
> reference page for the reset method.

There are three pages (at least) that show the construction of a stream, with
the third one being "Legacy Mode :: Random Numbers", which does not document
the limits on the seeds.

I see the logic now behind not having the seed limit in @RandStream . On the
other hand, he rand() documentation section that mentions legacy mode refers
the user to @RandStream for new work, and I think a lot of people are going to
miss the distinction between the @RandStream and RandStream (constructor)
documentation and think it is just another self-referential page (the rand()
page is self referential.)
```
 0
Reply Walter 6/7/2010 6:28:25 PM

```Hi Greg,

Thanks a lot. This surely helps. Could you please elaborate more on the below statement. I think I didn't get you correctly.

"> I don't know the constraints on the components
> of the 35-dim state vector. However, whatever
> they are, you could generate 2^128 of them by
> using 27 4-bit binary integers and 12 3-bit
> binary integers obtained from one 128-bit
> binary integers."

Does it talk about breaking the bit string into chunks and then feeding it to the random number generator? Is it possible to set the seed iteratively and get a single output stream from the random number generator? Can you please give me an example of how to perform the above mentioned method?

Thanks,
Randima
```
 0
Reply Randima 6/8/2010 3:53:03 AM

```On Jun 7, 11:53 pm, "Randima Hettiarachchi" <randi...@yahoo.com>
wrote:
> Hi Greg,
>
> Thanks a lot. This surely helps. Could you please elaborate
> more on the below statement. I think I didn't get you correctly.
>
> "> I don't know the constraints on the components
>
> > of the 35-dim state vector. However, whatever
> > they are, you could generate 2^128 of them by
> > using 27 4-bit binary integers and 12 3-bit
> > binary integers obtained from one 128-bit
> > binary integers."
>
> Does it talk about breaking the bit string into chunks and then
> feeding it to the random number generator?

What do you mean by "it"?? The documentation? If so
type

doc rand
help rand

and see that there is no reference to any bit string.

Please reread my previous reply. There are 2^1492 ordered states.
Each is characterized by a 35-dim state vector. Starting from any
state, a call of rand will automatically transition to the next
state. 2^1492 + 1 calls will return to the starting state.

I don't know the magic formula for determining state(i+1,:) from
state(i,:). Someone else will have to give you that reference.

There are 2^32 states (called "integer states") of the 2^1492 that
can be accessed using an integer in the closed interval [0,2^32-1].
I don't know how they are spaced within the 2^1492.

> Is it possible to set the seed iteratively and get a single
> output stream from the random number generator?

You choose a single seed. That will result in a stream of
2^1492 unique pseudorandom numbers before repeating the
sequence. Each call of rand will result in the next number
in the sequence.

If you save the 35-dim state vector at the end of an experiment
you can use it to start the next experiment. That is the best
guarantee that you will continue to get sequences of pseudorandom
numbers.

> Can you please give me an example of how to
> perform the above mentioned method?

No.

Reason 1: I don't see a reason for doing what you are asking.
You did not answer my previous question of why you
think this is needed.
Reason 2: If there is a legitimate reason for doing so, that
is an idea I would pursue. I have not gone beyond
that initial thought of decomposing a 2^128 bit
number into 35 smaller bit numbers and using those
numbers to create the 35-dim state vector.

Hope this helps.

Greg

```
 0
Reply heath (3875) 6/14/2010 1:23:53 AM

12 Replies
442 Views

(page loaded in 0.181 seconds)

Similiar Articles:

7/25/2012 10:35:04 PM