even E Kim arks New folder Matlab programming exercises ME34
Solution
%%% Program 1
clc;
clear all;
critical_num=input(\'Please Enter A Critical Number between 0 and 1 \ \');
i=1;
greater=0;
stop=0;
while(stop==0)
rand_num(i)=rand;
disp(rand_num(i));
if(rand_num(i)>critical_num)
greater=greater+1;
end
stop=input(\'Do you want to generate another random number(1=STOP, 0=CONTINUE)\ \');
i=i+1;
end
fprintf(\'Total Generated Random Numbers are : %d \ \',(i-1));
fprintf(\'Successful Generation of greater than critical number is : %d \ \',greater);
fprintf(\'Unsuccessful Generation of greater than critical number is : %d \ \',(i-1-greater));
%%%%Output 1
Please Enter A Critical Number between 0 and 1
0.45
0.7094
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.7547
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.2760
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.6797
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.6551
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.1626
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.1190
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.4984
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.9597
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.3404
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.5853
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.2238
Do you want to generate another random number(1=STOP, 0=CONTINUE)
0
0.7513
Do you want to generate another random number(1=STOP, 0=CONTINUE)
1
Total Generated Random Numbers are : 13
Successful Generation of greater than critical number is : 8
Unsuccessful Generation of greater than critical number is : 5
>>
------------------------------------------------------------------------------------------------------------------------------------------------------
%%% Program 2
clc;
clear all;
critical_num=input(\'Please Enter A Critical Number between 0 and 1 \ \');
tol=input(\'Enter the tolerance value \ \');
i=1;
greater=0;
stop=0;
while(stop==0)
rand_num(i)=rand;
disp(rand_num(i));
if(abs(rand_num(i)-critical_num)<tol)
break;
end
i=i+1;
end
fprintf(\'No of tries takens before falling random number in window area : %d \ \',(i-1));
Output 2:-
Please Enter A Critical Number between 0 and 1
0.15
Enter the tolerance value
0.01
0.4799
0.9047
0.6099
0.6177
0.8594
0.8055
0.5767
0.1829
0.2399
0.8865
0.0287
0.4899
0.1679
0.9787
0.7127
0.5005
0.4711
0.0596
0.6820
0.0424
0.0714
0.5216
0.0967
0.8181
0.8175
0.7224
0.1499
No of tries takens before falling random number in window area : 26
>>


