Conditional Random number generation

19 ビュー (過去 30 日間)
wang syr
wang syr 2017 年 3 月 2 日
コメント済み: Bruno Luong 2023 年 8 月 12 日
Hello there, For example; If I want to generate 5 random integer numbers with a sum of 20, how can I do that?
" ... example = ceil(10*rand(100, 5)) ... "
  1 件のコメント
Rik
Rik 2020 年 12 月 20 日
Why did you edit away your question? It is stored on Google cache anyway, so it's easy to restore.

サインインしてコメントする。

採用された回答

Roger Stafford
Roger Stafford 2017 年 3 月 4 日
function R = randfixedsumint(m,n,S);
% This generates an m by n array R. Each row will sum to S, and
% all elements are all non-negative integers. The probabilities
% of each possible set of row elements are all equal.
% RAS - Mar. 4, 2017
if ceil(m)~=m|ceil(n)~=n|ceil(S)~=S|m<1|n<1|S<0
error('Improper arguments')
else
P = ones(S+1,n);
for in = n-1:-1:1
P(:,in) = cumsum(P(:,in+1));
end
R = zeros(m,n);
for im = 1:m
s = S;
for in = 1:n
R(im,in) = sum(P(s+1,in)*rand<=P(1:s,in));
s = s-R(im,in);
end
end
end
return
  6 件のコメント
Yu Takahashi
Yu Takahashi 2021 年 2 月 9 日
編集済み: Walter Roberson 2021 年 2 月 10 日
Wondering whether it is possible to specify the max and min of the devided value? i.e., something like what you kindly provided in the randfixedsum function, thanks!
Ref
Bruno Luong
Bruno Luong 2023 年 8 月 12 日
@Walter Roberson "Wondering whether it is possible to specify the max and min of the devided value?"

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 3 月 2 日
  9 件のコメント
Walter Roberson
Walter Roberson 2017 年 3 月 4 日
Ah. I don't think I know how to implement your suggestion, though, at least not without generating all of the possible choices that sum to 20 and then picking one at random.
John D'Errico's https://www.mathworks.com/matlabcentral/fileexchange/12009-partitions-of-an-integer can calculate all of the possible partitions; a question is whether we can avoid having to take that step.
Walter Roberson
Walter Roberson 2017 年 3 月 4 日
https://en.wikipedia.org/wiki/Partition_(number_theory)#Restricted_part_size_or_number_of_parts talks about restricted partitioning briefly, and ties it to change making problems, which does indeed sound equivalent to the approach I was taking. Those are in turn tied to knapsack problems.

サインインしてコメントする。


Bruno Luong
Bruno Luong 2020 年 8 月 10 日
m = 5;
n = 3;
s = 10;
This will generate uniform distribution with sum criteria
% generate non-negative integer random (m x n) array row-sum to s
[~,r] = maxk(rand(m,s+n-1),n-1,2);
z = zeros(m,1);
r = diff([z, sort(r,2), (s+n)+z],1,2)-1;
  1 件のコメント
Bimal Ghimire
Bimal Ghimire 2020 年 10 月 4 日
While generating conditional random numbers, how can we generate random numbers that has a limit of some maximum value and have certain specified sum value?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by