Re: [R] random numbers selection - simple example

From: Ted Harding <ted.harding_at_nessie.mcc.ac.uk>
Date: Wed, 06 Jun 2007 16:24:21 +0100 (BST)


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 ------------------------------

______________________________________________
R-help_at_stat.math.ethz.ch 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. Received on Wed 06 Jun 2007 - 16:01:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Jun 2007 - 16:31:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.