[R] Random Number Generators and Sample

Romain Francois romain.francois at dbmail.com
Mon Aug 16 10:15:07 CEST 2010


Le 16/08/10 09:35, Linda Eaton a écrit :
>
> I am trying to get documentation about the random number generator used in
> "sample". The help for sample does not mention it. Can anyone point me in
> the right direction.
>
> Thanks

Hi,

You can follow the white rabbit into the code burrow.

 > sample
function (x, size, replace = FALSE, prob = NULL)
{
     if (length(x) == 1L && is.numeric(x) && x >= 1) {
         if (missing(size))
             size <- x
         .Internal(sample(x, size, replace, prob))
     }
     else {
         if (missing(size))
             size <- length(x)
         x[.Internal(sample(length(x), size, replace, prob))]
     }
}
<environment: namespace:base>

so you need to look for the internal sample, you can search for this in 
the file src/main/names.c in the R source, you'll see that line :

{"sample",	do_sample,	0,	11,	4,	{PP_FUNCALL, PREC_FN,	0}},

Then you grep for do_sample in the src/main directory, which leads you 
to the random.c file :

/* do_sample - probability sampling with/without replacement.
    .Internal(sample(n, size, replace, prob))
*/
SEXP attribute_hidden do_sample(SEXP call, SEXP op, SEXP args, SEXP rho)

You'll have all the details there.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2



More information about the R-help mailing list