I would like to to sample values from a vector s<-0:1440 to create a vector u so that the sum(u)=x while length(u)<k, for given k and x. Obviously k*max(s)>sum(u).
Is there any way to brute force simulate numerous such u vectors? I would like to avoid messing with probability distributions (for the sampling) and I don’t care if some u vectors will get discarded.
EDIT: Regarding P Lapointe’s nice comment about the length(u). It is important that the length(u) should not be fixed (length(u)<k) so that the vectors u are of variable lengths. Another approach would be to fix the length(u)=k, but the algorithm should be able to randomly insert (simulate) zeros in the u vectors. This would have the consequence that by adding a zero, the sum(u) remains the same but the length(u) increases by one (until length(u)=k). It is important that the zeros appear randomly (not just at the end of the simulated vector, just to satisfy length(u)=k)
OK, here’s an algo that answers your question. Basically, we are doing two random samples. The first one to find a k that satisfies the
length(u)<kconstraint. Using that k, we then use another sample to findk-1numbers. This is calledinitialin the algo. When we find a samplek-1which is lower thanx, the desired sum, we add the difference ofx-sum(initial)to complete the series.Also, if you have a large
max.k, it might be a good thing to add a probability vector that gives more probability to low numbers in the sample. Otherwise, in the current example, it is tough to get a sum==2500 if you have several numbers above 1000.