how to generate a random array list with fixed sum ?

10 visualizaciones (últimos 30 días)
tlig zied
tlig zied el 19 de Abr. de 2020
Comentada: Walter Roberson el 21 de Abr. de 2020
this code distributes fixed number in an array list, for exemple we have sum = 10, and the size of the array list is 3
the code generates this distribution [ 8 1 1] or other distribution...how can I modify this code to obtain a distribution that each value must be between 1 and sum/2 to obtain something like that [3 3 4]
S = 20;
n = 5;
m = 1;
while true
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
x = find(R<=0);
if x~=0
continue
else
break
end
end
indMaxArry = ceil(n/2);
R = circshift(sort(R),[0,indMaxArry])
  6 comentarios
tlig zied
tlig zied el 21 de Abr. de 2020
yes I want random INTEGERS

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 20 de Abr. de 2020
  2 comentarios
tlig zied
tlig zied el 21 de Abr. de 2020
I can't use it because of this constarint n*a <= s <= n*b
Matt J
Matt J el 21 de Abr. de 2020
You haven't told us what any of those symbols mean, but the FEX submission does support bounds on the array elements.

Iniciar sesión para comentar.

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by