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!