random number generation for known sum
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
i what do generate some 15 random numbers that sum for each separate value of say, 60 , 70, 40, 65. How would i go abouts this?
sum = [60 70 40 65];
n=15;
b = [ ];
for i = 1:1:n,
a = randi([1,??]);
b = [b a];
end
채택된 답변
Azzi Abdelmalek
2013년 8월 27일
n=60;
m=1:n;
a=m(sort(randperm(60,15)));
b=diff(a);
b(end+1)=60-sum(b)
sum(b)
댓글 수: 13
thanks Azzi, i get an error
Error in ==> Untitled at 3
a=m(sort(randperm(60,15)));
You must be using an older MATLAB that does not have that enhancement to randperm(). Try
t = randperm(n);
a=m(sort(t(1:15));
harley
2013년 8월 27일
thanks
Arnab Pal
2018년 11월 4일
I want to generate the same, but I need Non-integer numbers. Is it possible?
Bruno Luong
2018년 11월 4일
편집: Bruno Luong
2018년 11월 4일
@Arnab
sumtarget = 60;
n = 5;
x = diff([0,sort(randperm(sumtarget+n-1,n-1)),sumtarget+n])-1
Sir, It is generating the integer numbers only.
Bruno Luong
2018년 11월 4일
Ah sorry some how I read non-negative.
Tejas
2020년 7월 13일
Is there an easy way to control the maximum value each number in 'b' can have? Say, each value in 'b' must be less than or equal to 7, and still the 15 numbers in 'b' should add up to 60.
Roger's FEX contribution, the a and b parameters are lower bound and upper bound. You can use zeros for the lower bound if that is appropriate for your situation.
I want the numbers to be integers, as in the original question. Roger's FEX contribution seems to work with real numbers.
Bruno Luong
2020년 7월 13일
편집: Bruno Luong
2020년 7월 13일
The easiest way is perhaps using Roger FEX function, then do some sort of "integering" the float solution
sumatarget = 60
n = 15;
ub = 7;
x = floor([0; cumsum(randfixedsum(n,1,sumatarget,0,ub))]);
x(end) = sumatarget; % prevent floating point error
r = diff(x)
The distribution might be not perfectly uniform but possibly close enough and suitable for what ever you want to do with it.
Tejas
2020년 7월 14일
Integering the solution from Roger's function works very well for me. I do not require the numbers to be perfectly uniform. Thanks!
추가 답변 (1개)
Walter Roberson
2013년 8월 27일
2 개 추천
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
