MATLAB: Loop runs infinitely,

random number generatorwhile loop

% member ship value taken by zeros initilization
U=zeros(2,3);
% **********************************************

% each row is randomly initialize and
% must be 1 [ i.e., summation of complete row value]
% **********************************************
for i=1:2
% for each i is 1 to 2, iteratively increased row
while(1)
total=0;
for j=1:3
U(i,j)=rand();
% generaly random value is less than one
total=total+U(i,j);
% each row, every colomn value is added to variable 'total'
end
if (total == 1)
% check value is equal to one or not,example total=1.1235
% then total considers as 1 but my point is exact total
% varible must be 'one'
break;
end
end
end
disp(U);
loop runs infinite so that please describe how to stop it

Best Answer

Since total is random numbers, it will never equal exactly 1, so see if it exceeds 1 instead
if total >= 1
Of check within a tolerance
if abs(total-1) < someSmallNumber