How to generate repeatable random multiple vectors with Specific Number of Values and Fixed Sum?

2 views (last 30 days)
Hello everybody,
I want to create repeatable random multiple vectors with Specific Number of Values and Fixed Sum.
for example, in case of Specific Number of Values 3 and Fixed Sum 10,
If m row is 5, the random number can be [1; 2; 2; 4; 1], 1+2+2+4+1 =10 and Number of Values 3 (1,2 and 4 used)
or [1.1; 2.1; 2.1; 3.6; 1.1]. 1.1+2.1+2.1+3.6+1.1 =10 and Number of Values 3 (1.1, 2.1 and 3.6 used)
in case of Specific Number of Values 2 and Fixed Sum 10,
If m row is 4, the random number can be [2; 3; 3; 2], 2+3+3+2 =10 and Number of Values 2 (2 and 3 used)
or [1.5; 3.5; 3.5; 1.5]. 1.5+3.5+3.5+1.5 =10 and Number of Values 2 (1.5 and 3.5 used)
in case of Specific Number of Values 1 and Fixed Sum 10,
If m row is 4, the random number can be [2.5; 2.5; 2.5; 2.5]. 2.5+2.5+2.5+2.5 =10 and Number of Values 1 (2.5 only used)
Specific Number of Values 1 means only one value can be used,
I found the randfixedsum code provided by Roger Stafford. It is wonderful code, but it generate all random numers
without Specific Number of Values.
Please give me some help how to make the code for it.

Accepted Answer

Rik
Rik on 15 Sep 2022
Edited: Rik on 15 Sep 2022
You should try to restructure your problem to make it fit the tools you already have.
If we figure out a way to generate an order, we can use randfixedsum to generate the actual values.
If you have m=5,N=2,S=10, then you can first list all possible permutations:
m = 5;N = 2;S = 10;
rng(0) % fix random seed to make this example the same each time
A = rand;B = rand;
[A A A A B];
[A A A B B];
[A A B B B];
[A B B B B];
A and B must each occur at least once, so we only need to generate a list for the m-N remaining elements.
remaining_elements = randi(N,1,m-N)
remaining_elements = 1×3
1 2 2
Now we can count how often each element is selected:
Occurences_per_value = 1+histcounts(remaining_elements,0.5:(0.5+N))
Occurences_per_value = 1×2
2 3
Now let's actually generate some random numbers:
x = randfixedsum(N,1,S,0,S).'
x = 1×2
6.3925 3.6075
You will notice, this only results in N numbers, not m. The trick is to divide these numbers and replicate them:
x./Occurences_per_value
ans = 1×2
3.1962 1.2025
We can use repelem to replicate the numbers as often as we need them:
repelem('AB',Occurences_per_value)
ans = 'AABBB'
RandomData = repelem(x./Occurences_per_value,Occurences_per_value)
RandomData = 1×5
3.1962 3.1962 1.2025 1.2025 1.2025
But we have a last problem: these values are not randomly ordered. Once last call to randperm will fix that:
RandomData = RandomData(randperm(end))
RandomData = 1×5
3.1962 1.2025 1.2025 3.1962 1.2025
All in one compact (uncommented) block:
Occurences_per_value = 1+histcounts(randi(N,1,m-N),0.5:(0.5+N))
RandomData = repelem((randfixedsum(N,1,S,0,S).')./Occurences_per_value,Occurences_per_value)
RandomData = RandomData(randperm(end))
function [x,v] = randfixedsum(n,m,s,a,b)
% https://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
end

More Answers (1)

Bruno Luong
Bruno Luong on 15 Sep 2022
Edited: Bruno Luong on 15 Sep 2022
Just split randomly at most m times on top of randfixedsum output
m=5;
nvalues=3;
starget=10;
n=diff([0 sort(randperm(m-1,nvalues-1)) m]); % nvalues integers sum to m
r=randfixedsum(nvalues,1,starget,0,10); % nvalues positive numbers sum to starget
r=repelem(r./n',n)
r = 5×1
2.3823 2.3823 1.2863 1.9745 1.9745
  2 Comments
Bruno Luong
Bruno Luong on 15 Sep 2022
This code generate a slightly different statistics property. The difference between vallues has probably smaller rms than my forst code, if such difference is important for whatever simulation you do.
m=5;
nvalues=3;
starget=10;
i = sort(randperm(m-1,nvalues-1));
n = diff([0 i m]); % nvalues integers sum to m
r = randfixedsum(m,1,starget,0,10)'; % m positive numbers sum to starget
c = cumsum(r);
r = diff([0 c(i) starget]); % nvalues integers sum to starget
r = repelem(r./n, n)
r = 1×5
1.6626 1.6626 1.4038 2.6355 2.6355

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by