On Sat, Sep 4, 2010 at 8:07 AM, Yi <liuyi.fe...@gmail.com> wrote: > Sorry I forgot to talk about the range. > > But as an example, range (17,23) works. > > In your codes, mean is not exactly 20 and the samples are not integer.
The samples *are* integers. sample(17:23,10000,TRUE) returns integers. > However, what I want is integers with mean 20 exactly. > > Any tips? > Well, something will have to go. You can't have a random uniform sample of integers within a given range and have an exact mean every time. Suppose your range was -1 to 1, so possible values -1,0,1, and you want integer mean 0. The only way to do that is to have equal numbers of -1s and +1s in your sample, and the number of zeros is irrelevant - you could have 5000 zeroes and the mean would still be 0 if you had 25 -1s and 25 +1s - thats clearly not a uniform distribution, and you'll have to impose certain conditions if that's what you want. By extension, as long as you have an odd number of integers in your sample and you want the mean to be the median value (so in the 17:23 example, mean of 20) it is sufficient to generate the same number of 17s as 23s, the same number of 18s as 22s, the same number of 19s as 21s, and as many 20s as you like. Not exactly sure of the maths for non-median means, you'd have to pick fewer values on one side to cancel out the extra weight on the other. But given that this 'distribution' is going to be weird in many ways, perhaps you should answer the question: "Why?" Barry ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.