-
Notifications
You must be signed in to change notification settings - Fork 0
/
srswor.c
41 lines (32 loc) · 912 Bytes
/
srswor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "array.h"
#include "replaceR.h"
#include "mt19937ar.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 functions called SampleNoReplace, ProbSampleReplace,
ProbSampleReplace*/
/* SRS subset program written by Geoffrey Thompson 11/14/13
int srswor(unsigned int n, unsigned int k, unsigned int *y)
{
/* Provide k out of n indices sampled at random without replacement */
if (k > n) {
printf("Error: k greater than n in srswor()");
return 1;
}
else {
int i, j;
int *x;
MAKE_VECTOR(x, n);
for (i = 0; i < n; i++) x[i] = i;
for (i = 0; i < k; i++) {
j = randint(n);
y[i] = x[j];
x[j] = x[--n];
}
FREE_VECTOR(x);
}
return 0;
}