Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random number help

139 views
Skip to first unread message

Nishant

unread,
Sep 12, 2005, 3:40:02 AM9/12/05
to
Hello frnds,
I am new user to this group.

I need your guidelines about Random number
generation as I am unable to figure out the right technique to use
RAND function in Matlab.As in my problem it is giving me same
sequence of values in each iteration of the for loop.

Although I am using CLOCK function to vary my output with time
it does'nt seem to workproperly.
i have used some these functions
rand('state',sum(0.00000001*clock));
and
rand('state',sum(100*clock));

please guide me as to what is the best way to utilize RAND function
so that same sequence is not repeated every time.

Awaiting your reply

Thanks

Nishant

kb

unread,
Sep 12, 2005, 5:03:50 AM9/12/05
to
hi Nishant,

% setting the random seed is optional.
rand('state',sum(100*clock));
% or rand('state', 2005);

N=10;
for i=1:N
% to generate a random number b/n 0 and 1
x(i) = rand;
% ... and so on
end

% to generate a Nx2 matrix with random entries b/n 0 and 1
N=10;
y = rand(N,2)

% for more on uniform distribution
help rand

% on normal distribution
help randn.

KB

Steve Amphlett

unread,
Sep 12, 2005, 5:39:36 AM9/12/05
to
Nishant wrote:
>
>
>
> Although I am using CLOCK function to vary my output with
> time
> it does'nt seem to workproperly.
> i have used some these functions
> rand('state',sum(0.00000001*clock));

That's not going to work, because when converted to an integer,
sum(0.00000001*clock) is always zero.

> and
> rand('state',sum(100*clock));

This works for me though:

rand('state',sum(100*clock));
rand(1)

First time:
ans =

0.6802

Second time:
ans =

0.4704

Third time:
ans =

0.4772

Steve Amphlett

unread,
Sep 12, 2005, 5:49:06 AM9/12/05
to
Steve Amphlett wrote:
>
>
> This works for me though:
>
> rand('state',sum(100*clock));
> rand(1)
>
> First time:
> ans =
>
> 0.6802
>
> Second time:
> ans =
>
> 0.4704
>
> Third time:
> ans =
>
> 0.4772

I should add that I am quitting and restarting Matlab each time in
the above test.

Paul Skoczylas

unread,
Sep 12, 2005, 10:21:33 AM9/12/05
to
"Nishant" <nish...@rediffmail.com> wrote in message news:1126510802.1...@g47g2000cwa.googlegroups.com...


I'll add one point to the things already said by others:

Only set the state once! Do NOT do it for every random number you generate.

-Paul


LRM1138

unread,
Sep 12, 2005, 1:56:10 PM9/12/05
to

In other words, take it out of your FOR loop, if not out of your code
entirely.

Greg Heath

unread,
Sep 12, 2005, 9:29:45 PM9/12/05
to

For an integer state,

rand('state',J), (J = 1,2,...2^31 ).

For the rest of this year

2005+9+12 = 2026 < sum(clock) < 2005+12+31+23+59+60 = 2190.

Therefore choose

J = 1 + fix((sum(clock)-2026)*2^31/(2190-2026))

Hope this helps.

Greg

Nishant

unread,
Sep 13, 2005, 1:47:43 AM9/13/05
to
hello Friends
Thanks for your suggestions. But I am still unable
to debug my code.

Please suggest me regarding the best possible use of random number
generator.
I am unable to figure out the reason why it is not working properly.I
have seen that the random number generator works fine when I use less
number of iteration in the for loop but it fails to work properly when
I increase the number of iterations in the for loop.

meaning that the random number generator generates different
sequence of random no each time if the no of iterations in the for
loop is less where as it fails to produce a different sequence of
random no each time if the no of iterations in the for loop in large.


please let me know any technique to debug this error in my code
Awaiting your reply

Regards And Thanks
Nishant

Steve Amphlett

unread,
Sep 13, 2005, 4:18:37 AM9/13/05
to

Well, I wonder if anyone understood all that. I didn't. I think
it's time to post some code and to describe what's wrong with the
results it produces.

Nishant

unread,
Sep 13, 2005, 10:20:08 AM9/13/05
to
Hello friends,
At your request I am posting the code which
is used to generate cooccurance matrix for each iteration of for loop.
The problem here is that it generates same cooccurance matrix for each
iiteration in for loop.The cooccurance matrix depends on the sequence
of random no generated during one iteration of the for loop.Now what I
am unable to follow is that when I enter large value for PP and CC then
the random no generator does not work and it produces the same sequence
of random no for each single iteration in for loop - meaning I get the
same cooccurance matrix for each iteration of for loop. This implies I
get the sum cooccuranc e matrix with only two elements 0 and the no of
iterations of for loop say - 4 . But instead my actual result sum
cooccuracne matrix should have all the numbers between 0 and 4 for this
case viz 0,1,2 , 3 4,.

And what surprises me most is that the code works fine if the no
PP and CC is less than 3 or for say(a small no), but it fails to work
if PP and CC are larger nos say 10.

Please let me know if you find any flaw in my code or the method of
using random generator.

Awaiting ur reply

Thanks for your support

Nishant


PLz see code below

clc ;
close all;
clear all;
M = input('Enter the number of rows/columns for the square matrix =
');
r = input('Enter the number for the power r greater than zero in the
probability formula= ');
PP=input('Enter the number for the no of people N= ');
CC=input('Enter the number of samples n=');
CCM=0;
% rand('state',0);
% rand('state',sum(1*clock));
% s = rand('state');
% u1 = rand(M^2*M^2);
% rand('state',s);
% u2 = rand(M^2*M^2);

abc=1;dendfigno=PP+1;
for dd=1 : PP
disp('The information about person no.');
disp(dd);
% INPUT the number M, this serves as input argument for function call
%generate_M(M);
% rand('state',0);
% abc=1;
s='r.';
X = [];Y = [];Z=[];A=[];B=[];G=[];AR=[]; JJ=[];%G is used to store
group/cluster assignment information
for i=0 :M-1
for k= 0 : M-1
X = [X i];
Y = [Y k];
% XA(i+1,:)=
end

end
figure(dd);
axis equal;
t=(M-1)/2;
for m=0 : M-1
for l=0 : M-1
Z=[Z 0];

end
end

A=[X-t];
B=[Y-t];
AA=reshape(A,M,M);
BA=reshape(B,M,M);
% l = 1
% for j = 1: sqrt(length(A))
% for k = 1:sqrt(length(A))
% Asq(j,k) = A(1,l)
% l= l+1
% end
% end
% l=1
% for j = 1: sqrt(length(B))
% for k = 1:sqrt(length(B))
% Bsq(j,k) = B(1,l)
% l= l+1
% end
% end
plot(A,B,s,A,Z,'b-',Z,B,'g-');
% AA= Asq';
% BA= Bsq';
disp('X Coordinates MATRIX AA IS')
disp(AA)
disp('Y Coordinates MATRIX BA IS')
disp(BA)
temp=0;
for i=1 : M
for k=1 : M

x1=AA(M,M)-AA(i,k);
y1=BA(M,M)-BA(i,k);
x2=AA(M,1)-AA(i,k);
y2=BA(M,1)-BA(i,k);
x3=AA(1,1)-AA(i,k);
y3=BA(1,1)-BA(i,k);
x4=AA(1,M)-AA(i,k);
y4=BA(1,M)-BA(i,k);
d1=sqrt(x1^2+y1^2);
d2=sqrt(x2^2+y2^2);
d3=sqrt(x3^2+y3^2);
d4=sqrt(x4^2+y4^2);
D=[d1 d2 d3 d4];
%k=1;
disp('probabilities for point ')
disp(i);disp(','); disp(k);
s=0;

% if min(D)== 0
% SPL=i;
% rand('state',0);
% rand('state',sum(0.001*clock));
Y = rand(abc);
disp('The Random no is');
disp(Y);
jb=0;
dd=[];
if ((d1>0)&( d2>0)&(d3>0)&(d4>0))
jb=1;
expe=0;
for kk=1: 4
PN(kk)=(1/D(kk)^r)/(1/d1^r + 1/d2^r + 1/d3^r + 1/d4^r);
disp('INFORMATION ABOUT PROB DATA');
disp('FOR ASSIGNING A POINT TO GROUP/CLUSTER NO.');
disp(kk);
disp('THE THEORITICAL PROBABILITY IS');
disp(PN(kk));
s=s+ PN(kk);
dd=[dd s];

if (Y<=dd(kk))
expe=expe+1;
while expe==1
prob=Y;
disp('the point is assigned to cluster/group no.');
G(i,k)=kk;
disp(kk);
disp(' the randomly assigned probability of clustering to
the group is');
disp(prob);
expe=expe+1;
end
end
end
end
disp('the sum of probability to be assigned to either of groups is');
disp(s);

if jb==0
disp('special case');
n=[];
for m=1 : 4
% disp('Probability of going to group no');
% disp(k);
if D(m) == 0
temp=temp+1;
n=[n 1]; break,end;
P(m)=(1/D(m)^r)/(1/d1^r + 1/d2^r + 1/d3^r + 1/d4^r);
if P(m)== 0
n=[n 0];
continue
end

% Y = rand(size(t));
n(m)=abs(minus(P(m),Y));
s=s+ P(m);
% disp(P(k))
end
[C,I]=min(n); % not able to
assign proper group in case of case failure ie 0 case type
if C ==0
[C,I]=max(n);
% C=C+1;
% I=temp;
end
disp('the point is assigned to cluster/group no.');
G(i,k)=I;
disp(I);
disp('the probability of assigned to that group is');
disp(C);
end % end of bigger for loop


% % for m=1 : 4
% % % disp('Probability of going to group no');
% % % disp(k);
% % if D(m) == 0
% % P(m)=1, break,end
% % P(m)=(1/D(m)^r)/(1/d1^r + 1/d2^r + 1/d3^r + 1/d4^r);
% % s=s+ P(m);
% % % disp(P(k))
% % end
% % [C,I]=max(P)
% % disp('the point is assigned to cluster/group no.');
% % disp(I);
% % disp('the probability of assigned to that group is');
% % disp(C);
%
% disp(s)
end
end

AAAA= reshape(G',1,M^2);
KKK=eye(M^2);

for i = 1 : M^2
for j=1 : M^2
if AAAA(i)==AAAA(j)
KKK(i,j)=1;
KKK(j,i)=1;
end
end
end
JJ=KKK;
% return
disp('the sample co-occurance matrix is as follows');% GIVING SAME
RESULT FOR EACH ITERATION
disp(KKK)
disp('check-the M X M matrix containing group info is ');
disp(G)
disp('check-the 1 X M matrix containing group info is ');
disp(AAAA)
X= KKK;
Y= pdist(X,'cityblock');
Z= linkage(Y,'average');
figure(dendfigno);
[H,T] = dendrogram(Z,'colorthreshold','default');
% H = dendrogram(Z);
CCM= CCM + JJ;
dendfigno=dendfigno+1;
end % end of main for loop
disp('The sum co-occurance matrix is=');% REPETITION OF NUMBERS SO
DEFECTIVE
disp(CCM);
X= CCM;
Y= pdist(X,'cityblock');
Z= linkage(Y,'average');
figure(99);
[H,T] = dendrogram(Z,'colorthreshold','default');
sum_ccm = sum(CCM,1);
disp(sum_ccm);
Ch = nchoosek(PP,CC);
% k=1;t=k+1;ctr=1;
% for i= 1 : Ch
% for j=1 : 2
% if j==1
% AR(i,j)=k;
% end
%
% if j==2
% AR(i,j)=t;
% t=t+1;
% end
% if t==PP+1
% t=k+2;
% k=k+1;
% end
%
% end

Op=nchoosek(1:1:PP,CC);

disp(Op);

% end


disp(AR)
Kl = randperm(Ch);
Ab=Kl(1);
Aj=Op(Ab,:);
disp('SAMPLING BEGINS for the following elements ');
disp(Aj);
ACCM=0;k2k=1;dendfigno=CC+1
% for dd=Aj(1): Aj(CC)
for dd=1:CC
% rand('state',0);
% rand('state',sum(100*clock));
disp('The information about person no.');
disp(Aj(dd));
% INPUT the number M, this serves as input argument for function call
%generate_M(M);
% rand('state',0);
% rand('state',sum(100*clock));
% abc=1;
s='r.';
X = [];Y = [];Z=[];A=[];B=[];G=[];AR=[]; JJ=[];%G is used to store
group/cluster assignment information
for i=0 :M-1
for k= 0 : M-1
X = [X i];
Y = [Y k];
% XA(i+1,:)=
end

end
figure(dd);
axis equal;
t=(M-1)/2;
for m=0 : M-1
for l=0 : M-1
Z=[Z 0];

end
end

A=[X-t];
B=[Y-t];
AA=reshape(A,M,M);
BA=reshape(B,M,M);
% l = 1
% for j = 1: sqrt(length(A))
% for k = 1:sqrt(length(A))
% Asq(j,k) = A(1,l)
% l= l+1
% end
% end
% l=1
% for j = 1: sqrt(length(B))
% for k = 1:sqrt(length(B))
% Bsq(j,k) = B(1,l)
% l= l+1
% end
% end
plot(A,B,s,A,Z,'b-',Z,B,'g-');
% AA= Asq';
% BA= Bsq';
disp('X Coordinates MATRIX AA IS')
disp(AA)
disp('Y Coordinates MATRIX BA IS')
disp(BA)
temp=0;
for i=1 : M
for k=1 : M

x1=AA(M,M)-AA(i,k);
y1=BA(M,M)-BA(i,k);
x2=AA(M,1)-AA(i,k);
y2=BA(M,1)-BA(i,k);
x3=AA(1,1)-AA(i,k);
y3=BA(1,1)-BA(i,k);
x4=AA(1,M)-AA(i,k);
y4=BA(1,M)-BA(i,k);
d1=sqrt(x1^2+y1^2);
d2=sqrt(x2^2+y2^2);
d3=sqrt(x3^2+y3^2);
d4=sqrt(x4^2+y4^2);
D=[d1 d2 d3 d4];
%k=1;
disp('probabilities for point ')
disp(i);disp(','); disp(k);
s=0;

% if min(D)== 0
% SPL=i;
% rand('state',0);
% rand('state',sum(100*clock));
Y = rand(abc);
disp('The Random no is');
disp(Y);
jb=0;
dd=[];
if ((d1>0)&&( d2>0)&&(d3>0)&&(d4>0))
jb=1;
expe=0;
for kk=1: 4
PN(kk)=(1/D(kk)^r)/(1/d1^r + 1/d2^r + 1/d3^r + 1/d4^r);
disp('INFORMATION ABOUT PROB DATA');
disp('FOR ASSIGNING A POINT TO GROUP/CLUSTER NO.');
disp(kk);
disp('THE THEORITICAL PROBABILITY IS');
disp(PN(kk));
s=s+ PN(kk);
dd=[dd s];

if (Y<=dd(kk))
expe=expe+1;
while expe==1
prob=Y;
disp('the point is assigned to cluster/group no.');
G(i,k)=kk;
disp(kk);
disp(' the randomly assigned probability of clustering to
the group is');
disp(prob);
expe=expe+1;
end
end
end
end
disp('the sum of probability to be assigned to either of groups is');
disp(s);

if jb==0
disp('special case');
n=[];
for m=1 : 4
% disp('Probability of going to group no');
% disp(k);
if D(m) == 0
temp=temp+1;
n=[n 1]; break,end;
P(m)=(1/D(m)^r)/(1/d1^r + 1/d2^r + 1/d3^r + 1/d4^r);
if P(m)== 0
n=[n 0];
continue
end

% Y = rand(size(t));
n(m)=abs(minus(P(m),Y));
s=s+ P(m);
% disp(P(k))
end
[C,I]=min(n); % not able to
assign proper group in case of case failure ie 0 case type
if C ==0
[C,I]=max(n);
% C=C+1;
% I=temp;
end
disp('the point is assigned to cluster/group no.');
G(i,k)=I;
disp(I);
disp('the probability of assigned to that group is');
disp(C);
end % end of bigger for loop


% % for m=1 : 4
% % % disp('Probability of going to group no');
% % % disp(k);
% % if D(m) == 0
% % P(m)=1, break,end
% % P(m)=(1/D(m)^r)/(1/d1^r + 1/d2^r + 1/d3^r + 1/d4^r);
% % s=s+ P(m);
% % % disp(P(k))
% % end
% % [C,I]=max(P)
% % disp('the point is assigned to cluster/group no.');
% % disp(I);
% % disp('the probability of assigned to that group is');
% % disp(C);
%
% disp(s)
end
end

AAAA= reshape(G',1,M^2);
KKK=eye(M^2);

for i = 1 : M^2
for j=1 : M^2
if AAAA(i)==AAAA(j)
KKK(i,j)=1;
KKK(j,i)=1;
end
end
end
JJ=KKK;
% return
disp('the sample co-occurance matrix is as follows');% GIVING SAME
RESULT FOR EACH ITERATION
disp(KKK)
disp('check-the M X M matrix containing group info is ');
disp(G)
disp('check-the 1 X M matrix containing group info is ');
disp(AAAA)
X= KKK;
Y= pdist(X,'cityblock');
Z= linkage(Y,'average');
figure(dendfigno);
[H,T] = dendrogram(Z,'colorthreshold','default');
ACCM= ACCM + JJ;
disp(k2k);
k2k=k2k+1;
disp('check');
disp(ACCM);

end % end of main for loop
disp('The sum co-occurance matrix for given sample is=');% REPETITION
OF NUMBERS SO DEFECTIVE
disp(ACCM);
X= ACCM;
Y= pdist(X,'cityblock');
Z= linkage(Y,'average');
figure(100);
[H,T] = dendrogram(Z,'colorthreshold','default');
disp('for the sample no');
disp(Aj);

% end % end main for loop

Steve Amphlett

unread,
Sep 13, 2005, 10:32:38 AM9/13/05
to
Nishant wrote:
>
>
<snip, much code (much of it commented out)...

You should be manually setting your seed one per Matlab session, not
from within your code. Try putting it into your startup.m or
something.

Nishant

unread,
Sep 14, 2005, 12:59:49 AM9/14/05
to
Hi there,
I am entering the simpler code so that it is easy to see
through.


M = input('Enter the number of rows/columns for the square matrix =
');
r = input('Enter the number for the power r greater than zero in the
probability formula= ');
PP=input('Enter the number for the no of people N= ');
CC=input('Enter the number of samples n=');
CCM=0;
% rand('state',0);
% rand('state',sum(1*clock));

abc=1;
for dd=1 : PP % main for loop or for number of persons


for i=1 : M
for k=1 : M


Y = rand(abc);
disp(Y);
end
end % these 2
for loops produces a Matrix of random numbers


end


In the above code I am getting same sequence(same matrix) of random
numbers for each iteration of the main for loop (ie for dd=1:PP) in
case PP is large number. I have tried using random number generators(
viz % rand('state',0);
% rand('state',sum(100*clock))) but both of them did not deliver the
desired results.


Desired Result: different sequence( Matrix) of random numbers for each
iteration of main for loop(or for no of persons entered)


Please let me know if i can use RAND function in some manner to
produced the desired results


Also let me know what is seeds?

Steve Amphlett

unread,
Sep 14, 2005, 4:33:11 AM9/14/05
to
Nishant wrote:
>
>
<snip, more code but STILL no actual vs desired outputs...

If I enter 2 to all the questions asked, I get this screen output:

0.1389

0.2028

0.1987

0.6038

0.2722

0.1988

0.0153

0.7468

And if I do it again, I get:

0.4451

0.9318

0.4660

0.4186

0.8462

0.5252

0.2026

0.6721

Note that no two numbers above are the same. What do you get and
what would you prefer to get?

Steven Lord

unread,
Sep 14, 2005, 1:08:49 PM9/14/05
to

"Nishant" <nish...@rediffmail.com> wrote in message
news:1126673989....@o13g2000cwo.googlegroups.com...

> Hi there,
> I am entering the simpler code so that it is easy to see
> through.

*snip code*

In your code, just before the line where you call RAND, add this command:

which -all rand

Make sure that when this command runs, the output does not say "rand is a
variable", and that it also only lists the RAND that ships with MATLAB (the
one under $MATLAB/toolbox/matlab/elmat, where $MATLAB is your MATLAB root
directory.) If it lists some other function named RAND, remove or rename
that function or move the directory containing that function to the bottom
of your MATLAB path. This will make sure you're using the RAND function
that ships with MATLAB.

--
Steve Lord
sl...@mathworks.com


Nishant

unread,
Sep 14, 2005, 7:06:49 PM9/14/05
to
Hello friends,
Thanks for your suggestions on random no generator.

But it still doesn't show desired results (viz generating a different
sequence of numbers after each iteration of for loop).
I think what is happening is that the command
rand('state',sum(100*clock)); is not working effectively.
Please give me some suggestions on using RAND function more effectively
so that I can set the Random generator to a different state each time
RAND function is called.

Plz see the code and the sample output below

Seeking your opinion

Thanks

Nishant

SAMPLE CODE:
clc()
close all


M = input('Enter the number of rows/columns for the square matrix = ');

PP=input('Enter the number for the no of people N= ');
which -all rand
% rand('state',0);
% rand('state',sum(100*clock));


abc=1;
for dd=1 : PP % main for loop or for number of persons

which -all rand
% rand('state',0);
rand('state',sum(100*clock));

disp('the information about person no');
disp(dd);


for i=1 : M
for k=1 : M

% rand('state',0);
% rand('state',sum(100*clock));

Y = rand(abc);
disp(Y);
end
end % these 2

% for loops produces a Matrix of random numbers
%

end

The Output shown on Matlab Command
Window
Enter the number of rows/columns for the square matrix = 2
Enter the number for the no of people N= 2
C:\MATLAB701\toolbox\matlab\elmat\rand.bi
C:\MATLAB701\toolbox\matlab\elmat\rand.m % Shadowed
C:\MATLAB701\toolbox\matlab\elmat\rand.bi
C:\MATLAB701\toolbox\matlab\elmat\rand.m % Shadowed
the information about person no
1

0.2517

0.0165

0.1520

0.5500

C:\MATLAB701\toolbox\matlab\elmat\rand.bi
C:\MATLAB701\toolbox\matlab\elmat\rand.m % Shadowed
the information about person no
2

0.2517

0.0165

0.1520

0.5500

>>

Nishant

unread,
Sep 14, 2005, 7:57:06 PM9/14/05
to
Hi Friends,
Thanks for your suggestions
Ahah! I think I finally got the right solution to my problem. I was
doing a silly mistake by not this command in my program
randn('state',sum(100*clock));

instead i was wrongly using rand('state',sum(100*clock));

Thanks for your guidance and support

Regards

Nishant

Nishant

unread,
Sep 15, 2005, 2:12:43 AM9/15/05
to
Hi frnds,
Thanks for your reply.
Can u suggest me how should i develop programs in matlab for >
generating dendrogram(A dendrogram is a 'tree-like' diagram that
summaries the process of clustering).


I wish to mention the fact that the I am looking for dendrogram type
structure which provides information about the height of the dendrite
formations and the grouping or clustering information for each
dendrite.


Please let me know about the details I can get using dendrogram
command in matlab.


Awaiting your reply


Thanks

Nishant

unread,
Sep 15, 2005, 2:12:49 AM9/15/05
to

Nishant

unread,
Sep 15, 2005, 2:12:52 AM9/15/05
to

Nishant

unread,
Sep 16, 2005, 6:51:58 PM9/16/05
to
hello friends,
I am struck with random number generator again.

This time with the function rand('state',sum(100*clock))- meaning this
function is not producing different state of random number every time
whenever it is called. Thus I am getting same sequence of numbers each
time.

This is the sample code of matlab.

plz see the code and let me know how should I use the function
rand('state',sum(100*clock))- to get different sequence of numbers
each time

Looking forward to your guidance

Thanks

Nishant

SAMPLE MAT LAB CODE :
clc()
close all
rand('seed',0)


M = input('Enter the number of rows/columns for the square matrix = ');

PP=input('Enter the number for the no of people N= ');
which -all rand


% rand('state',0);
% rand('state',sum(100*clock));

abc=1;
for dd=1 : PP % main for loop or for number of persons
which -all rand

% rand('state',0);
% randn('state',sum(100*clock));
% randn('state',sum(100*clock));
rand('state',sum(100*clock))
% rand('state-codeword');
% rand('state',dd);

disp('the information about person no');
disp(dd);

for i=1 : M
for k=1 : M

% rand('state',0);
% rand('state',sum(100*clock));

Y = rand(abc);

beli...@aol.com

unread,
Sep 16, 2005, 7:06:13 PM9/16/05
to
Nishant wrote:
> Hi frnds,
> Thanks for your reply.
> Can u suggest me how should i develop programs in matlab for >
> generating dendrogram(A dendrogram is a 'tree-like' diagram that
> summaries the process of clustering).

I think you should improve your usage of Usenet.

(1) You posted separate but almost identical messages to
comp.soft-sys.matlab and sci.stat.math on random number generators. If
your message must be sent to multiple newsgroups, it is better to
cross-post.

(2) You ask a new, unrelated question about dendrograms in an old
thread. A new topic should have a new thread.

(3) You post multiple copies of the same message.

(4) You use silly misspellings such as "frnds", "u", "i".

You will get better answers and waste less time of other people if you
use Usenet more carefully.

Greg Heath

unread,
Sep 17, 2005, 7:00:02 AM9/17/05
to

Nishant wrote:
> hello friends,
> I am struck with random number generator again.
>
> This time with the function rand('state',sum(100*clock))- meaning this
> function is not producing different state of random number every time
> whenever it is called. Thus I am getting same sequence of numbers each
> time.
>
> This is the sample code of matlab.
>
> plz see the code and let me know how should I use the function
> rand('state',sum(100*clock))- to get different sequence of numbers
> each time
>
> Looking forward to your guidance

1. Instead of a small number like 100, you need a large number
like 1e6.
2. Go back and read my previous post.
3. You only have to initialize the state once at the beginning
of your ptogram.

Hope this helps.

Greg

Nishant

unread,
Sep 18, 2005, 11:56:28 AM9/18/05
to
Hello Greg,
Thanks for your message. But I am still unable to
get the correct results, even after using large numbers like 1e6.


Is there any other way to improve the working of command
rand('state',sum(100*clock))- .

Looking forward to your guidance

Thanks

Nishant

Nishant

unread,
Sep 18, 2005, 11:56:33 AM9/18/05
to
Hello Greg,
Thanks for your message. But I am still unable to
get the correct results, even after using large numbers like 1e6.


Is there any other way to improve the working of command
rand('state',sum(100*clock))- .

Looking forward to your guidance

Thanks

Nishant

Nishant

unread,
Sep 18, 2005, 11:56:38 AM9/18/05
to
Hello Greg,
Thanks for your message. But I am still unable to
get the correct results, even after using large numbers like 1e6.


Is there any other way to improve the working of command
rand('state',sum(100*clock))- .

Looking forward to your guidance

Thanks

Nishant

John D'Errico

unread,
Sep 18, 2005, 12:57:24 PM9/18/05
to
In article <1127058998....@g49g2000cwa.googlegroups.com>,
"Nishant" <nish...@rediffmail.com> wrote:

> Hello Greg,
> Thanks for your message. But I am still unable to
> get the correct results, even after using large numbers like 1e6.
>
>
> Is there any other way to improve the working of command
> rand('state',sum(100*clock))- .

Why do you feel a need to set the seed at all?

How have you determined the random number generator
is yielding incorrect results?

John


--
The best material model of a cat is another, or
preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

per isakson

unread,
Sep 18, 2005, 1:03:39 PM9/18/05
to

I have reproduced your result.

I believe you get identical "random" numbers because of a combination
of the speed of your computer and the resolution of the function
CLOCK; sum( 100*clock ) returns the same number every time. If you
try with a large enough number for PP (M=2 and PP=200 on my computer)
you will see different "random" numbers for PP=1 and PP=200.

Try the following:

1. insert the command pause(1) before the end of the outer loop. This
will give you different "random" numbers for each value of PP, which
supports my explanation.

2. move the command rand('state',sum(100*clock))to the beginning of
the script, as suggested before by others.

The command rand( 'seed', 0 ) has no effect since it is overtaken by
rand( 'state', ... ).

/ per

Greg Heath

unread,
Sep 19, 2005, 6:02:30 AM9/19/05
to
Nishant wrote:
> Hello Greg,
> Thanks for your message. But I am still unable to
> get the correct results, even after using large numbers like 1e6.
>
> Is there any other way to improve the working of command
> rand('state',sum(100*clock)) .

>
> Looking forward to your guidance

1. rand('state',J) for *one* J in the set (J = 1,2,...2^31)
is used to *initialize a sequence* of pseudo-random numbers.
2. It is usually only called *once* in a program, regardless
of how many numbers are to be generated.
3. Now

min(J) = 1
max(J) = 2^31 = 2147483648 = 2147.483648e6

and, from Jan 1, 2005 t0 Dec 31, 2005

min(sum(clock)) = 2005 + 1 + 1 + 0 + 0 + 0 = 2007
max(sum(clock)) = 2005 + 12 + 31 + 23 + 59 + 59 = 2189.

Therefore,

2007e2 <= J = sum(100*clock) <= 2189e2

which is a pitifully small interval of 18200 compared to
2^31-1 = 2147183647.
4. Unfortunately, blindly using 1e6 (as I recommended) will
not work because, at least once a day,
sum(1e6*clock) > 2^31.
5. However, blindly using 1e5 will work.
6. Alternatively, almost all of the initial states can
be generated, over the span of this year, by using

J = floor( 1 + (clock-2007)/182]*(2^31-1) )
~ floor( 1 + 1.1799360e7*(clock-2007 ) )
7. Regardless of how you pick J, as long as it is in the
interval 1<= J <= 2^31, you will get a valid
pseudo-random sequence.
8. If you continually change initialization states
within the program, there is no guarantee that the
conctenation of the sequences will be pseudorandom.

Hope this helps.

Greg

Nishant

unread,
Sep 19, 2005, 11:24:36 AM9/19/05
to
Hello Per isakson,
Thanks for useful suggestions.
Your solutions works fine for producing different sequnce for each
iteration of for loop.

I think I was I was mistaken in using the rand('state',sum(100*clock))
inside the for loop. As I thought this command sets the state to
different mode each time it will be executed inside the for loop. But
now I reaslise that the only way rand function works is using outiside
all loops.

Also your use of pause command is a good technique when using clock
function

Scott Seidman

unread,
Sep 19, 2005, 11:32:53 AM9/19/05
to
"Nishant" <nish...@rediffmail.com> wrote in news:1127143476.347527.141410
@g14g2000cwa.googlegroups.com:

>
> I think I was I was mistaken in using the rand('state',sum(100*clock))
> inside the for loop. As I thought this command sets the state to
> different mode each time it will be executed inside the for loop. But
> now I reaslise that the only way rand function works is using outiside
> all loops.

This is what people have been telling you for about a week. Why the sudden
change of heart??

--
Scott
Reverse name to reply

0 new messages