[R] How to generate integers from uniform distribution with

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat Sep 4 09:56:45 CEST 2010


There is still ambiguity (and I think some misunderstanding)
in your query! First, Barry's code does yield integers as the
values in the sample. As a smaller illustrative example:

  x <- sample(17:23,20,TRUE)

will give results like

  x
  # [1] 21 17 23 21 17 17 19 18 17 17 17 22 20 23 20 20 18 20 19 20

which are all integers.

Secondly, in general, the mean of the sampled numbers will not be 20
exactly, even though their *expected* mean is 20:

  mean(x)
  # [1] 19.3

Barry gave an example of a sample size so large that the mean would
very probably be extremely close to 20 (20.0057 when he did it).
This will of course vary from sample to sample:

  mean(sample(17:23,10000,TRUE))
  # [1] 19.9991
  mean(sample(17:23,10000,TRUE))
  # [1] 20.031
  mean(sample(17:23,10000,TRUE))
  # [1] 20.0207
  mean(sample(17:23,10000,TRUE))
  # [1] 19.9819

You say: "However, what I want is integers with mean 20 exactly."
This is ambiguous. On the one hand, Barry's procedure samples
integers from (17,18,19,20,21,22,23) with equal probability,
a distribution which has mean exactly 20 *as the distribution
which is being sampled from*, although the mean of the values
in any particular sample will very probably not be exactly 20.
So, in that sense, Barry's procedure does give you a *method*
of sampling integers which has mean 20 exactly".

On the other hand, a possible interpretation of what you say
is that you want every sample to be such that, after you have
obtained the sample (say 'x'), then mean(x) = 20 exactly (as
opposed to what you will get from Barry's code, where the mean
will be close to, but almost never equal to, 20).

If that is what you want, then it is more tricky to acieve.
You are then effectively sampling from the conditional distribution:
X1, X2, ... , Xn uniformly distributed on (17:23) conditional on
X1 + X2 + ... Xn = 20*n.

This can be done, but before working out how to do it one would
need to be assured that this really is what you mean!

Ted.

On 04-Sep-10 07:07:41, Yi 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.
> However, what I want is integers with mean 20 exactly.
> 
> Any tips?
> Thanks
> 
> On Thu, Sep 2, 2010 at 12:16 AM, Barry Rowlingson <
> b.rowlingson at lancaster.ac.uk> wrote:
> 
>>  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
>>
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 04-Sep-10                                       Time: 08:56:41
------------------------------ XFMail ------------------------------



More information about the R-help mailing list