comparison sample.cpp @ 284:cacad987d785

Really finish with the sampling branch, this time merging all of it, yes, even the last revision. (The last revision was one that seeded the RNG with the current time, which is helpful when trying to get multiple independent-ish samples from the same database...)
author mas01cr
date Mon, 07 Jul 2008 08:57:06 +0000
parents 0e44de38d675
children b9eff6896943
comparison
equal deleted inserted replaced
283:0e44de38d675 284:cacad987d785
34 } 34 }
35 35
36 return c; 36 return c;
37 } 37 }
38 38
39 unsigned audioDB::random_track(unsigned *propTable, unsigned total, gsl_rng *rng) { 39 unsigned audioDB::random_track(unsigned *propTable, unsigned total) {
40 /* FIXME: make this O(1) by using the alias-rejection method, or 40 /* FIXME: make this O(1) by using the alias-rejection method, or
41 some other sensible method of sampling from a discrete 41 some other sensible method of sampling from a discrete
42 distribution. */ 42 distribution. */
43 double thing = gsl_rng_uniform(rng); 43 double thing = gsl_rng_uniform(rng);
44 unsigned sofar = 0; 44 unsigned sofar = 0;
54 return 0; 54 return 0;
55 } 55 }
56 56
57 void audioDB::sample(const char *dbName) { 57 void audioDB::sample(const char *dbName) {
58 initTables(dbName, 0); 58 initTables(dbName, 0);
59
60 gsl_rng *rng = gsl_rng_alloc(gsl_rng_mt19937);
61
62 /* FIXME: in Real Life we'll want to initialize the RNG using
63 /dev/random or the current time or something, like this:
64
65 unsigned int seed;
66 int fd = open("/dev/urandom", O_RDONLY);
67 read(fd, &seed, 4);
68
69 gsl_rng_set(rng, seed);
70 */
71 59
72 // build track offset table (FIXME: cut'n'pasted from query.cpp) 60 // build track offset table (FIXME: cut'n'pasted from query.cpp)
73 off_t *trackOffsetTable = new off_t[dbH->numFiles]; 61 off_t *trackOffsetTable = new off_t[dbH->numFiles];
74 unsigned cumTrack=0; 62 unsigned cumTrack=0;
75 for(unsigned int k = 0; k < dbH->numFiles; k++){ 63 for(unsigned int k = 0; k < dbH->numFiles; k++){
103 91
104 double sumdist = 0; 92 double sumdist = 0;
105 double sumlogdist = 0; 93 double sumlogdist = 0;
106 94
107 for (unsigned int i = 0; i < nsamples;) { 95 for (unsigned int i = 0; i < nsamples;) {
108 unsigned track1 = random_track(propTable, total, rng); 96 unsigned track1 = random_track(propTable, total);
109 unsigned track2 = random_track(propTable, total, rng); 97 unsigned track2 = random_track(propTable, total);
110 98
111 if(track1 == track2) 99 if(track1 == track2)
112 continue; 100 continue;
113 101
114 unsigned i1 = gsl_rng_uniform_int(rng, propTable[track1]); 102 unsigned i1 = gsl_rng_uniform_int(rng, propTable[track1]);