random number generation for known sum
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 件のコメント
harley
2013 年 8 月 27 日
thanks Azzi, i get an error
Error in ==> Untitled at 3
a=m(sort(randperm(60,15)));
Walter Roberson
2013 年 8 月 27 日
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.
Walter Roberson
2020 年 7 月 13 日
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 日
カテゴリ
ヘルプ センター および 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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
