[R] random numbers selection - simple example

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Wed Jun 6 17:24:21 CEST 2007


On 06-Jun-07 14:30:44, Jenny Barnes wrote:
> Dear R-help,
> 
> Which random number generator function would you recommend for
> simply picking 15 random numbers from the sequence 0-42? I want
> to use replacement (so that the same number could potentially be
> picked more than once).

R has the function sample() which samples a given number of items
from a given set, without replacement by default, but with replacement
if you specify this. Enter

  ?sample

for more information. In the above case

  sample((0:42), 15, replace=TRUE)

will do what you seem to describe above. Example:

> sample((0:42), 15, replace=TRUE)
 [1] 26 38  1 41 11 30 22 37 28  0  0 25 10 39 27

if you want them in random order (i.e. "as they come off the line"),
or

> sort(sample((0:42), 15, replace=TRUE))
 [1]  1  3  5  8  8 10 16 17 21 25 30 30 33 34 40

if you want them sorted.

Best wishes,
Ted.

> I have read the R-help archives and the statistics and computing
> book on modern Applied statistics with S but the advice seems to
> be for much form complicated examples, there must be a simpler way
> for what I am trying to do?
> 
> If anybody can help me I would greatly appreciate your advice and time,
> 
> Best Wishes,
> 
> Jenny

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 06-Jun-07                                       Time: 16:24:15
------------------------------ XFMail ------------------------------



More information about the R-help mailing list