[R] How to generate integers from uniform distribution with fixed mean

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Sep 2 09:16:26 CEST 2010


On Thu, Sep 2, 2010 at 7:17 AM, Yi <liuyi.feier at gmail.com> wrote:
> Hi, folks,
>
> runif (n,min,max) is the typical code for generate R.V from uniform dist.
>
> But what if we need to fix the mean as 20, and we want the values to be
> integers only?

 It's not clear what you want. Uniformly random integers with expected
mean 20 - but what range? Any range centred on 20 will work, for
example you could use sample() with replacement. To see the
distribution, use sample()

 table(sample(17:23,10000,TRUE))

 which gives a uniform distribution of integers from 17 to 23, so the
mean is 20.0057 for 10000 samples.

 Is that what you want?

Barry



More information about the R-help mailing list