utils.h
00001
00002 #ifndef _UTILS_H_
00003 #define _UTILS_H_
00004
00005 #include <vector>
00006 #include <stdlib.h>
00007
00008 namespace librf {
00009
00010 void random_sample(int n, int K, vector<int>*v, unsigned int* seed) {
00011 if (K < n) {
00012 int pop = n;
00013 v->reserve(K);
00014 for (int i = K; i > 0; --i) {
00015 float cumprob = 1.0;
00016 float x = float(rand_r(seed))/RAND_MAX;
00017 for (; x < cumprob; pop--) {
00018 cumprob -= cumprob * i /pop;
00019 }
00020 v->push_back(n - pop - 1);
00021 }
00022 } else {
00023 for (int i =0; i < n; i++) {
00024 v->push_back(i);
00025 }
00026 }
00027 }
00028
00029 }
00030 #endif
Generated on Mon Jan 8 23:19:06 2007 for librf by
1.3.7