How to generate random number within a sum limit?
이전 댓글 표시
Generate 5 random numbers, where their sum should be less/equal to 50.
채택된 답변
추가 답변 (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);
카테고리
도움말 센터 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!