In reply to Ksharp.

Thanks for writing and sharing your code. It looks like your code tries to find an 800-element vector of integers on [2,3000] such that the mean and median are close to $291 and $100, respectively. You are using least-squares optimization to attain that objective. You are then thinking of the vectors as a possible set of data that satisfies the reported statistics.

This differs from my goal, which is to fit a distribution to the data. With a distribution, you can model the data and even simulate additional data sets of different sizes. However, your objective function (SSQ(mean(x)-291,median(x)-100)) reminds me that there are other ways to fit a distribution to the data.

One issue you could face with your method is non-uniqueness. For example, consider the smaller problem of a 3-element integer vector on the interval [0,10] where the mean is 3 and the median is 2. The vectors {1,2,6} and {0,2,7} both satisfy the objective function.