How to generate the sum of a random number ?

6 views (last 30 days)
  2 Comments
KALYAN ACHARJYA
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

Sign in to comment.

Accepted Answer

David Hill
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
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.
Yash Shil Balgobin
Yash Shil Balgobin on 2 Apr 2021
Edited: Yash Shil Balgobin on 2 Apr 2021
@Bruno Luong If you didn't read the above comments (including mine and David's), don't even try. Thank you.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!