-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleNoReplace.c
42 lines (34 loc) · 1.05 KB
/
SampleNoReplace.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
/***************************************************************/
/* Program: randunif.c
By: Brad Duthie
Description: Will sample size "samp" from Vec without replace
Compile: gcc randpois.c -ansi -Wall -pedantic */
/***************************************************************/
#include "randunif.h" /* Need random unform numbers */
#include "array.h" /* Array used to handle vectors */
void SampleWithoutReplacement(int Pop, int samp, int *Vec){
int n = samp;
int N = Pop;
int t = 0; /* total input records dealt with */
int m = 0; /* number of items selected so far */
double u;
while (m < n){
u = randunif();
if ((N - t)*u >= n - m ){
t++;
}else{
Vec[m] = t;
t++;
m++;
}
}
}
void shufflesamp(int *Vec, int samp){
int i, j, temp;
for (i = samp-1; i > 0; i--){
j = floor(randunif()*(i+1));
temp = Vec[i];
Vec[i] = Vec[j];
Vec[j] = temp;
}
}