How to generate the sum of a random number ?
6 views (last 30 days)
Show older comments
Yash Shil Balgobin
on 2 Apr 2021
Edited: Yash Shil Balgobin
on 2 Apr 2021
2 Comments
KALYAN ACHARJYA
on 2 Apr 2021
Edited: KALYAN ACHARJYA
on 2 Apr 2021
sum_num=0;
while sum_num<8
num=randn
if num>3
fprintf('The random number is greater than 3');
break;
end
sum_num=sum_num+num;
end
Accepted Answer
David Hill
on 2 Apr 2021
n = randn;
x = 0;
while x <= 9
n = randn;%needs to be in the loop
if n > 3
fprintf('The random number is greater than 3.\n');
break;
end
x=x+n;
end
fprintf('The sum of the random numbers is %f.\n', x);
fprintf('The last random number generated is %f.\n', n);
7 Comments
Bruno Luong
on 2 Apr 2021
Edited: Bruno Luong
on 2 Apr 2021
Not because you are beginner, but because you are someone who does not lake any effort to learn and/or trying to cheat in assignment (at least how it shows).
That is life, you'll be hated whereever you go if you stay in this line.
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!