I presume you mean sampling without replacement? The following
(adapted from the file you asked for) will do it, but you will need to
incorporate Rmath as standalone in order to get runif to work.

This function will give you k indices from n, sampled WOR. Thus, n = 50
for you and k = 5. You will get a vector y of length 5 (a pointer of int
actually) which will contain these indices.

Thus you will define a vector z (of length 50) which is 1:50, and then
using the function, your SRWOR sample will be z[y[i]] where i = 0,
1,...4.

I haven't tried this function out much myself, so YMMV.

HTH!

Best wishes,
Ranjan



#include <stdlib.h>
#ifndef USING_RLIB
#define MATHLIB_STANDALONE 1 /*It is essential to have this before the call 
                               to the Rmath's header file because this decides
                               the definitions to be set. */
#endif
#include <Rmath.h>

/* Note that use of this function involves a prior call to the Rmath library to
   get the seeds in place. It is assumed that this is done in the calling 
   function. */

/* Equal probability sampling; without-replacement case */
/* Adapted from the R function called SampleNoReplace */

/**
 * Stores k out of n indices sampled at random without replacement
 * in y.
 */
int srswor(int n, int k, int *y)
{ 
  if (k > n) {
    return 1;
  }
  else {
    const double len = (double) n;
    int i;
    int* x = malloc(n * sizeof(int));
    if (!x) {
      return 2;
    }
                
    for (i = 0; i < n; ++i)     x[i] = i;
    
    for (i = 0; i < k; ++i) {
      const int j = (int)(len * runif(0.0, 1.0));
      y[i] = x[j];
      x[j] = x[--n];
    }
    free(x);
  }
  return 0;
}



On Sun, 5 Apr 2009 20:11:04 +0100 Paul Smith <phh...@gmail.com> wrote:

> Thanks, Ranjan. Got it!
> 
> I am now wondering whether there is some simpler way of implementing
> the following in C:
> 
> sample(1:50,5)
> 
> Paul
> 
> 
> On Sun, Apr 5, 2009 at 4:10 PM, Ranjan Maitra <mai...@iastate.edu> wrote:
> > Hi Paul,
> >
> > It is in the main/src/random.c file of the source code.
> >
> > HTH!
> > Best wishes,
> > Ranjan
> >
> > On Sun, 5 Apr 2009 15:35:25 +0100 Paul Smith <phh...@gmail.com> wrote:
> >
> >> Dear All,
> >>
> >> I would like to use the function sample() in a program written in C.
> >> Is there somewhere the code of sample() written in C?
> >>
> >> Thanks in advance,
> >>
> >> Paul
> >>
> >> ______________________________________________
> >> R-help@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.
> >>
> >
> > ______________________________________________
> > R-help@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.
> >
> 
> ______________________________________________
> R-help@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.
>

______________________________________________
R-help@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.

Reply via email to