Random sampling to give an exact sum
I want to sample 140 numbers between 1000 to 100000 such that the sum of these 140 numbers is around 2 million (2000000): sample(1000:100000,140) such that: sum(sample(1000:100000,140)) = 2000000 Any pointers how I can achieve this? There exist an algorithm for generating such random numbers. Originally created for MATLAB , there is an R implementation of it: Surrogate::RandVec Citation from MATLAB script comment: % This generates an n by m array x, each of whose m columns % contains n random values lying in the interval [a,b], but % subject to the condition that their sum be equal to s. The %