How to generate random number within a sum limit?
5 次查看(过去 30 天)
显示 更早的评论
Generate 5 random numbers, where their sum should be less/equal to 50.
0 个评论
采纳的回答
更多回答(1 个)
Roger Stafford
2017-11-7
If the numbers are supposed to be any five positive integers, do this:
b = true;
while b
x = 50*randi(5,1);
b = sum(x)>50;
end
If the numbers can be any non-negative real numbers, change the third line above to:
x = 50*rand(5,1);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!