/* * * Von Neumann extractor for uniform * boolean values from random bit stream * */ void UniformDistribution::updateBooleanPool2() { while( m_bools.empty() ) { unsigned long rnd = gsl_rng_get(m_rng); unsigned long rnd2 = gsl_rng_get(m_rng); unsigned int offset = (sizeof(unsigned long) * 8); rnd ^= rnd2; do { if( rnd & (1 << offset) ) { m_bools.push( rnd2 & (1 << offset) ); } } while( --offset ); } }
void rng_read_write_test (const gsl_rng_type * T) { unsigned long int test_a[N], test_b[N]; int i; gsl_rng *r = gsl_rng_alloc (T); for (i = 0; i < N; ++i) { gsl_rng_get (r); /* throw away N iterations */ } { /* save the state to a binary file */ FILE *f = fopen("test.dat", "wb"); gsl_rng_fwrite(f, r); fclose(f); } for (i = 0; i < N; ++i) { test_a[i] = gsl_rng_get (r); } { /* read the state from a binary file */ FILE *f = fopen("test.dat", "rb"); gsl_rng_fread(f, r); fclose(f); } for (i = 0; i < N; ++i) { test_b[i] = gsl_rng_get (r); } { int status = 0; for (i = 0; i < N; ++i) { status |= (test_b[i] != test_a[i]); } /*gsl_test (status, "%s, random number generator read and write",*/ /*gsl_rng_name (r));*/ } gsl_rng_free (r); }
void rng_test (const gsl_rng_type * T, unsigned long int seed, unsigned int n, unsigned long int result) { gsl_rng *r = gsl_rng_alloc (T); unsigned int i; unsigned long int k = 0; int status; if (seed != 0) { gsl_rng_set (r, seed); } for (i = 0; i < n; i++) { k = gsl_rng_get (r); } status = (k != result); printf ( "%s, %u steps (%u observed vs %u expected)", gsl_rng_name (r), n, k, result); /* gsl_test (status, "%s, %u steps (%u observed vs %u expected)", gsl_rng_name (r), n, k, result); */ gsl_rng_free (r); }
int rng_min_test (gsl_rng * r, unsigned long int *kmin, unsigned long int ran_min, unsigned long int ran_max) { unsigned long int actual_uncovered; double expect_uncovered; int status; unsigned long int min = 1000000000UL; int i; for (i = 0; i < N2; ++i) { unsigned long int k = gsl_rng_get (r); if (k < min) min = k; } *kmin = min; actual_uncovered = min - ran_min; expect_uncovered = (double) ran_max / (double) N2; status = (min < ran_min) || (actual_uncovered > 7 * expect_uncovered); return status; }
void rgb_timing(Test **test, Rgb_Timing *timing) { double total_time,avg_time; int i,j; unsigned int *rand_uint; MYDEBUG(D_RGB_TIMING){ printf("# Entering rgb_timing(): ps = %u ts = %u\n",test[0]->psamples,test[0]->tsamples); } seed = random_seed(); gsl_rng_set(rng,seed); rand_uint = (uint *)malloc((size_t)test[0]->tsamples*sizeof(uint)); total_time = 0.0; for(i=0;i<test[0]->psamples;i++){ start_timing(); for(j=0;j<test[0]->tsamples;j++){ rand_uint[j] = gsl_rng_get(rng); } stop_timing(); total_time += delta_timing(); } avg_time = total_time/(test[0]->psamples*test[0]->tsamples); timing->avg_time_nsec = avg_time*1.0e+9; timing->rands_per_sec = 1.0/avg_time; free(rand_uint); }
static unsigned long gsl_rng_uint32 (gsl_rng *r) /* the uniform distribution on 0..2^{32}-1 */ { unsigned long min = gsl_rng_min (r); unsigned long max = gsl_rng_max (r); if (min == 0 && max == 4294967295U) { /* we have full 32 bit values */ return gsl_rng_get (r); } else { assert (max-min >= 65536); /* make sure we have at least 16 bit */ unsigned long a = (gsl_rng_get (r)-min)&0xFFFF; unsigned long b = (gsl_rng_get (r)-min)&0xFFFF; return (a<<16)|b; } }
static PyObject * normal(PyObject *self, PyObject *args, PyObject *kwrds) { matrix *obj; int i, nrows, ncols = 1; double m = 0, s = 1; char *kwlist[] = {"nrows", "ncols", "mean", "std", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwrds, "i|idd", kwlist, &nrows, &ncols, &m, &s)) return NULL; if (s < 0.0) PY_ERR(PyExc_ValueError, "std must be non-negative"); if ((nrows<0) || (ncols<0)) { PyErr_SetString(PyExc_TypeError, "dimensions must be non-negative"); return NULL; } if (!(obj = Matrix_New(nrows, ncols, DOUBLE))) return PyErr_NoMemory(); gsl_rng_env_setup(); rng_type = gsl_rng_default; rng = gsl_rng_alloc (rng_type); gsl_rng_set(rng, seed); for (i = 0; i < nrows*ncols; i++) MAT_BUFD(obj)[i] = gsl_ran_gaussian (rng, s) + m; seed = gsl_rng_get (rng); gsl_rng_free(rng); return (PyObject *)obj; }
static PyObject * uniform(PyObject *self, PyObject *args, PyObject *kwrds) { matrix *obj; int i, nrows, ncols = 1; double a = 0, b = 1; char *kwlist[] = {"nrows", "ncols", "a", "b", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwrds, "i|idd", kwlist, &nrows, &ncols, &a, &b)) return NULL; if (a>b) PY_ERR(PyExc_ValueError, "a must be less than b"); if ((nrows<0) || (ncols<0)) PY_ERR_TYPE("dimensions must be non-negative"); if (!(obj = (matrix *)Matrix_New(nrows, ncols, DOUBLE))) return PyErr_NoMemory(); gsl_rng_env_setup(); rng_type = gsl_rng_default; rng = gsl_rng_alloc (rng_type); gsl_rng_set(rng, seed); for (i= 0; i < nrows*ncols; i++) MAT_BUFD(obj)[i] = gsl_ran_flat (rng, a, b); seed = gsl_rng_get (rng); gsl_rng_free(rng); return (PyObject *)obj; }
/* * * Iterated Von Neumann extractor for uniform * boolean values from random bit stream * */ void UniformDistribution::updateBooleanPool() { while( m_bools.empty() ) { unsigned long rnd = gsl_rng_get(m_rng); unsigned int offset = (sizeof(unsigned long) * 8); while( offset >= 2 ) { unsigned long input = rnd & 3; rnd /= 4; offset -= 2; switch( input ) { case 1: m_bools.push( false ); // append 1 to bools ++offset; rnd |= (1 << offset); break; case 2: m_bools.push( true ); ++offset; break; default: break; } } } }
int rng_max_test (gsl_rng * r, unsigned long int *kmax, unsigned long int ran_max) { unsigned long int actual_uncovered; double expect_uncovered; int status; unsigned long int max = 0; int i; for (i = 0; i < N2; ++i) { unsigned long int k = gsl_rng_get (r); if (k > max) max = k; } *kmax = max; actual_uncovered = ran_max - max; expect_uncovered = (double) ran_max / (double) N2; status = (max > ran_max) || (actual_uncovered > 7 * expect_uncovered) ; return status; }
void rng_parallel_state_test (const gsl_rng_type * T) { unsigned long int test_a[N], test_b[N]; unsigned long int test_c[N], test_d[N]; double test_e[N], test_f[N]; int i; gsl_rng *r1 = gsl_rng_alloc (T); gsl_rng *r2 = gsl_rng_alloc (T); for (i = 0; i < N; ++i) { gsl_rng_get (r1); /* throw away N iterations */ } gsl_rng_memcpy (r2, r1); /* save the intermediate state */ for (i = 0; i < N; ++i) { /* check that there is no hidden state intermixed between r1 and r2 */ test_a[i] = gsl_rng_get (r1); test_b[i] = gsl_rng_get (r2); test_c[i] = gsl_rng_uniform_int (r1, 1234); test_d[i] = gsl_rng_uniform_int (r2, 1234); test_e[i] = gsl_rng_uniform (r1); test_f[i] = gsl_rng_uniform (r2); } { int status = 0; for (i = 0; i < N; ++i) { status |= (test_b[i] != test_a[i]); status |= (test_c[i] != test_d[i]); status |= (test_e[i] != test_f[i]); } /*gsl_test (status, "%s, parallel random number state * consistency",*/ /*gsl_rng_name (r1));*/ } gsl_rng_free (r1); gsl_rng_free (r2); }
/* take a step through the TSP space */ void Stsp(const gsl_rng * r, void *xp, double step_size) { int x1, x2, dummy; int *route = (int *) xp; step_size = 0 ; /* prevent warnings about unused parameter */ /* pick the two cities to swap in the matrix; we leave the first city fixed */ x1 = (gsl_rng_get (r) % (N_CITIES-1)) + 1; do { x2 = (gsl_rng_get (r) % (N_CITIES-1)) + 1; } while (x2 == x1); dummy = route[x1]; route[x1] = route[x2]; route[x2] = dummy; }
void free_gsl_rng(gsl_rng *r, Args *args) { FILE *fp; if(args->s == 0) { fp = fopen("randomSeed.dat","w"); fprintf(fp,"%ld\n",gsl_rng_get(r)); fclose(fp); } gsl_rng_free(r); }
void rng_state_test (const gsl_rng_type * T) { unsigned long int test_a[N], test_b[N]; int i; gsl_rng *r = gsl_rng_alloc (T); gsl_rng *r_save = gsl_rng_alloc (T); for (i = 0; i < N; ++i) { gsl_rng_get (r); /* throw away N iterations */ } gsl_rng_memcpy (r_save, r); /* save the intermediate state */ for (i = 0; i < N; ++i) { test_a[i] = gsl_rng_get (r); } gsl_rng_memcpy (r, r_save); /* restore the intermediate state */ gsl_rng_free (r_save); for (i = 0; i < N; ++i) { test_b[i] = gsl_rng_get (r); } { int status = 0; for (i = 0; i < N; ++i) { status |= (test_b[i] != test_a[i]); } /*gsl_test (status, "%s, random number state consistency",*/ /*gsl_rng_name (r));*/ } gsl_rng_free (r); }
/* Document-method: <i>GSL::Rng#get</i> Returns a random integer from the generator. The minimum and maximum values depend on the algorithm used, but all integers in the range [min,max] are equally likely. The values of min and max can determined using the auxiliary methodss GSL::Rng#max and GSL::Rng#min. */ static VALUE rb_gsl_rng_get(int argc, VALUE *argv, VALUE obj) { gsl_rng *r = NULL; gsl_vector_int *v; size_t n, i; Data_Get_Struct(obj, gsl_rng, r); switch (argc) { case 0: return UINT2NUM(gsl_rng_get(r)); break; case 1: n = NUM2INT(argv[0]); v = gsl_vector_int_alloc(n); for (i = 0; i < n; i++) gsl_vector_int_set(v, i, (int) gsl_rng_get(r)); return Data_Wrap_Struct(cgsl_vector_int, 0, gsl_vector_int_free, v); break; default: rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc); break; } }
int main( void ) { /* Create hash table */ LALBitset *bs = XLALBitsetCreate(); XLAL_CHECK_MAIN( bs != NULL, XLAL_EFUNC ); /* Create some random bits */ BOOLEAN XLAL_INIT_DECL( bits, [4096] ); gsl_rng *r = gsl_rng_alloc( gsl_rng_mt19937 ); XLAL_CHECK_MAIN( r != NULL, XLAL_ESYS ); int nbits = 0; for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) { bits[n] = ( gsl_rng_uniform( r ) > 0.44 ); nbits += bits[n] ? 1 : 0; } /* Create random index offset into bitset */ const UINT8 n0 = gsl_rng_get( r ) % 65536; /* Print information */ printf("nbits = %i, n0 = %"LAL_UINT8_FORMAT"\n", nbits, n0); /* Set bits */ for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) { XLAL_CHECK_MAIN( XLALBitsetSet( bs, n0 + n, bits[n] ) == XLAL_SUCCESS, XLAL_EFUNC ); } /* Get bits */ for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) { BOOLEAN is_set = 0; XLAL_CHECK_MAIN( XLALBitsetGet( bs, n0 + n, &is_set ) == XLAL_SUCCESS, XLAL_EFUNC ); XLAL_CHECK_MAIN( !is_set == !bits[n], XLAL_EFAILED, "Inconsistent bit at index %"LAL_UINT8_FORMAT": LALBitset=%i, reference=%i", n0 + n, is_set, bits[n] ); } /* Clear bitset */ XLAL_CHECK_MAIN( XLALBitsetClear( bs ) == XLAL_SUCCESS, XLAL_EFUNC ); for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) { BOOLEAN is_set = 0; XLAL_CHECK_MAIN( XLALBitsetGet( bs, n0 + n, &is_set ) == XLAL_SUCCESS, XLAL_EFUNC ); XLAL_CHECK_MAIN( !is_set, XLAL_EFAILED, "Bit still set at index %"LAL_UINT8_FORMAT, n0 + n ); } /* Cleanup */ gsl_rng_free( r ); XLALBitsetDestroy( bs ); /* Check for memory leaks */ LALCheckMemoryLeaks(); return EXIT_SUCCESS; }
void *operateOnTree(void* tArgs) { struct timespec s,e; int chooseOperation; unsigned long lseed; int threadId; struct threadArgs* tData = (struct threadArgs*) tArgs; threadId = tData->threadId; lseed = tData->lseed; const gsl_rng_type* T; gsl_rng* r; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc(T); gsl_rng_set(r,lseed); while(!start) { } clock_gettime(CLOCK_REALTIME,&s); for(int i=0;i< (int)iterations;i++) { chooseOperation = gsl_rng_uniform(r)*100; if(chooseOperation < findPercent) { lookup(tData,gsl_rng_get(r)%keyRange + 1); } else if (chooseOperation < insertPercent) { insert(tData,gsl_rng_get(r)%keyRange + 1); } else { remove(tData,gsl_rng_get(r)%keyRange + 1); } } clock_gettime(CLOCK_REALTIME,&e); timeArray[threadId] = (double) (diff(s,e).tv_sec * 1000000000 + diff(s,e).tv_nsec)/1000; return NULL; }
static inline double rn_uniform(struct _flow *flow) { #ifndef HAVE_LIBGSL UNUSED_ARGUMENT(flow); #endif /* HAVE_LIBGSL */ #ifdef HAVE_LIBGSL gsl_rng * r = flow->r; return gsl_rng_get(r); #else return rand(); #endif /* HAVE_LIBGSL */ }
void evolve_pop(GSLrng_t * rng, std::vector<std::shared_ptr<singlepop_t> > * pops,const std::vector<unsigned> & nlist,const double & theta, const double & rho) { vector<thread> threads; std::vector<GSLrng_t> rngs; for( unsigned i = 0 ; i < pops->size() ; ++i ) { rngs.emplace_back(GSLrng_t(gsl_rng_get(rng->get()))); } for( unsigned i = 0 ; i < pops->size() ; ++i ) { threads.push_back( thread(evolve_pop_details,rngs[i].get(),pops->operator[](i).get(),nlist,theta,rho) ); } for(unsigned i=0;i<threads.size();++i) threads[i].join(); }
void rng_float_test (const gsl_rng_type * T) { gsl_rng *ri = gsl_rng_alloc (T); gsl_rng *rf = gsl_rng_alloc (T); double u, c ; unsigned int i; unsigned long int k = 0; int status = 0 ; do { k = gsl_rng_get (ri); u = gsl_rng_get (rf); } while (k == 0) ; c = k / u ; for (i = 0; i < N2; i++) { k = gsl_rng_get (ri); u = gsl_rng_get (rf); if (c*k != u) { status = 1 ; break ; } } /*gsl_test (status, "%s, ratio of int to double (%g observed vs %g expected)",*/ /*gsl_rng_name (ri), c, k/u);*/ gsl_rng_free (ri); gsl_rng_free (rf); }
int main(void) { const gsl_rng_type * T; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T); printf ("generator type: %s\n", gsl_rng_name (r)); printf ("seed = %lu\n", gsl_rng_default_seed); printf ("first value = %lu\n", gsl_rng_get (r)); gsl_rng_free (r); return EXIT_SUCCESS; }
void makesw(int **rede, unsigned long int sem) { // Constrói a rede small-world com base numa lista de vizinhança, em que o primeiro elemento é a conectividade do sítio int i, j, nex, nrand; double ran; gsl_rng *r; // iniciar semente r=gsl_rng_alloc(gsl_rng_mt19937); gsl_rng_set(r,sem); for(i=0;i<n;i++) { for(j=1;j<=K;j++) { ran=gsl_rng_uniform(r); if(ran<p) { nrand=(i+K+1+gsl_rng_get(r)%(n-K-1))%n; // Aloca para poder ter espaço, i-->nrand rede[i][0]++; // Incrementa a conectividade rede[i]=(int *)realloc(rede[i],(rede[i][0]+2)*I); rede[i][rede[i][0]]=nrand; // Aloca para poder ter espaço, nrand-->i rede[nrand][0]++; // Incrementa a conectividade rede[nrand]=(int *)realloc(rede[nrand],(rede[nrand][0]+2)*I); rede[nrand][rede[nrand][0]]=i; } else { nex=(i+j)%n; // Aloca para poder ter espaço, i-->nex rede[i][0]++; rede[i]=(int *)realloc(rede[i],(rede[i][0]+2)*I); rede[i][rede[i][0]]=nex; // Aloca para poder ter espaço, nex-->i rede[nex][0]++; rede[nex]=(int *)realloc(rede[nex],(rede[nex][0]+2)*I); rede[nex][rede[nex][0]]=i; } } } gsl_rng_free(r); }
/* * generating random numbers separately to * prevent the side effects on hardware counters. * * */ void generate_random_numbers(int *sequence) { const gsl_rng_type * T; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T); /* seed */ gsl_rng_set(r, seed); /* default generator type is mt19937, which is what we need */ // printf("generator type: %s\n", gsl_rng_name (r)); int i; for (i = 0; i < SEQUENCE_CNT; i++) { sequence[i] = gsl_rng_get(r) % SEQUENCE_CNT; // limit the random numbers to [0, 3999] // printf("%u\t", sequence[i]); } }
int init_xrandom(string init) { #ifdef HAVE_GSL char my_gsl_type[64], my_gsl_seed[64], *cp; /* need two strings, bug in putenv? */ string *is; int nis, iseed; is = burststring(init,", "); /* parse init as "[seed[,name]]" */ nis = xstrlen(is,sizeof(string))-1; if (nis > 0) { /* seed is first, but optional */ iseed = natoi(is[0]); if (iseed > 0 || streq(is[0],"+0")) { sprintf(my_gsl_seed,"%s=%s",env_seed,is[0]); } else { iseed = set_xrandom(iseed); sprintf(my_gsl_seed,"%s=%u",env_seed,iseed); } putenv(my_gsl_seed); dprintf(1,"putenv: %s\n",my_gsl_seed); if (nis > 1) { /* name is second, also optional */ sprintf(my_gsl_type,"%s=%s",env_type,is[1]); putenv(my_gsl_type); dprintf(1,"putenv: %s\n",my_gsl_type); } } gsl_rng_env_setup(); /* initialize the rng (name/seed) setup */ my_T = gsl_rng_default; my_r = gsl_rng_alloc(my_T); dprintf(1,"GSL generator type: %s\n",gsl_rng_name(my_r)); dprintf(1,"GSL seed = %u\n",gsl_rng_default_seed); dprintf(1,"GSL first value = %u\n",gsl_rng_get(my_r)); return (int) gsl_rng_default_seed; #else return set_xrandom(init? natoi(init) : 0); /* 18/06/2008: allow for init=0 WD */ #endif }
int diehard_runs(Test **test, int irun) { int i,j,k,t; unsigned int ucount,dcount; int upruns[RUN_MAX],downruns[RUN_MAX]; double uv,dv,up_pks,dn_pks; uint first, last, next = 0; /* * This is just for display. */ test[0]->ntuple = 0; test[1]->ntuple = 0; /* * Clear up and down run bins */ for(k=0;k<RUN_MAX;k++){ upruns[k] = 0; downruns[k] = 0; } /* * Now count up and down runs and increment the bins. Note * that each successive up counts as a run of one down, and * each successive down counts as a run of one up. */ ucount = dcount = 1; if(verbose){ printf("j rand ucount dcount\n"); } first = last = gsl_rng_get(rng); for(t=1;t<test[0]->tsamples;t++) { next = gsl_rng_get(rng); if(verbose){ printf("%d: %10u %u %u\n",t,next,ucount,dcount); } /* * Did we increase? */ if(next > last){ ucount++; if(ucount > RUN_MAX) ucount = RUN_MAX; downruns[dcount-1]++; dcount = 1; } else { dcount++; if(dcount > RUN_MAX) dcount = RUN_MAX; upruns[ucount-1]++; ucount = 1; } last = next; } if(next > first){ ucount++; if(ucount > RUN_MAX) ucount = RUN_MAX; downruns[dcount-1]++; dcount = 1; } else { dcount++; if(dcount > RUN_MAX) dcount = RUN_MAX; upruns[ucount-1]++; ucount = 1; } /* * This ends a single sample. * Compute the test statistic for up and down runs. */ uv=0.0; dv=0.0; if(verbose){ printf(" i upruns downruns\n"); } for(i=0;i<RUN_MAX;i++) { if(verbose){ printf("%d: %7d %7d\n",i,upruns[i],downruns[i]); } for(j=0;j<RUN_MAX;j++) { uv += ((double)upruns[i] - test[0]->tsamples*b[i])*(upruns[j] - test[0]->tsamples*b[j])*a[i][j]; dv += ((double)downruns[i] - test[0]->tsamples*b[i])*(downruns[j] - test[0]->tsamples*b[j])*a[i][j]; } } uv /= (double)test[0]->tsamples; dv /= (double)test[0]->tsamples; /* * This NEEDS WORK! It isn't right, somehow... */ up_pks = 1.0 - exp ( -0.5 * uv ) * ( 1.0 + 0.5 * uv + 0.125 * uv*uv ); dn_pks = 1.0 - exp ( -0.5 * dv ) * ( 1.0 + 0.5 * dv + 0.125 * dv*dv ); MYDEBUG(D_DIEHARD_RUNS) { printf("uv = %f dv = %f\n",uv,dv); } test[0]->pvalues[irun] = gsl_sf_gamma_inc_Q(3.0,uv/2.0); test[1]->pvalues[irun] = gsl_sf_gamma_inc_Q(3.0,dv/2.0); MYDEBUG(D_DIEHARD_RUNS) { printf("# diehard_runs(): test[0]->pvalues[%u] = %10.5f\n",irun,test[0]->pvalues[irun]); printf("# diehard_runs(): test[1]->pvalues[%u] = %10.5f\n",irun,test[1]->pvalues[irun]); } return(0); }
int dab_dct(Test **test,int irun) { double *dct; unsigned int *input; double *pvalues = NULL; unsigned int i, j; unsigned int len = (ntuple == 0) ? 256 : ntuple; int rotAmount = 0; unsigned int v = 1<<(rmax_bits-1); double mean = (double) len * (v - 0.5); /* positionCounts is only used by the primary test, and not by the * fallback test. */ double *positionCounts; /* The primary method is a chisq; we want expected counts of at least * five. If the number of tsamples is too low for that, use the * fallback method, which is doing kstest across the pvalues. */ int useFallbackMethod = (test[0]->tsamples > 5 * len) ? 0 : 1; /* ptest, v, and sd are only used in the fall-back method, when * tsamples is too small compared to ntuple. */ Xtest ptest; double sd = sqrt((1.0/6.0) * len) * v; dct = (double *) malloc(sizeof(double) * len); input = (unsigned int *) malloc(sizeof(unsigned int) * len); positionCounts = (double *) malloc(sizeof(double) * len); if (useFallbackMethod) { pvalues = (double *) malloc(sizeof(double) * len * test[0]->tsamples); } /* Zero out the counts initially. */ memset(positionCounts, 0, sizeof(double) * len); test[0]->ntuple = len; /* When used, the data is normalized first. */ ptest.y = 0.0; ptest.sigma = 1.0; /* Main loop runs tsamples times. During each iteration, a vector * of length ntuple will be read from the generator, so a total of * (tsamples * ntuple) words will be read from the RNG. */ for (j=0; j<test[0]->tsamples; j++) { unsigned int pos = 0; double max = 0; /* Change the rotation amount after each quarter of the samples * have been used. */ if (j != 0 && (j % (test[0]->tsamples / 4) == 0)) { rotAmount += rmax_bits/4; } /* Read (and rotate) the actual rng words. */ for (i=0; i<len; i++) { input[i] = gsl_rng_get(rng); input[i] = RotL(input[i], rotAmount); } /* Perform the DCT */ fDCT2_fft(input, dct, len); /* Adjust the first value (the DC coefficient). */ dct[0] -= mean; dct[0] /= sqrt(2); // Experimental + guess; seems to be correct. if (!useFallbackMethod) { /* Primary method: find the position of the largest value. */ for (i=0; i<len; i++) { if (fabs(dct[i]) > max) { pos = i; max = fabs(dct[i]); } } /* And record it. */ positionCounts[pos]++; } else { /* Fallback method: convert all values to pvalues. */ for (i=0; i<len; i++) { ptest.x = dct[i] / sd; Xtest_eval(&ptest); pvalues[j*len + i] = ptest.pvalue; } } } if (!useFallbackMethod) { /* Primary method: perform a chisq test for uniformity * of discrete counts. */ double p; double *expected = (double *) malloc(sizeof(double) * len); for (i=0; i<len; i++) { expected[i] = (double) test[0]->tsamples / len; } p = chisq_pearson(positionCounts, expected, len); test[0]->pvalues[irun] = p; free(expected); } else { /* Fallback method: perform a ks test for uniformity of the * continuous p-values. */ test[0]->pvalues[irun] = kstest(pvalues, len * test[0]->tsamples); } nullfree(positionCounts); nullfree(pvalues); /* Conditional; only used in fallback */ nullfree(input); nullfree(dct); return(0); }
int random_int() { return gsl_rng_get(rand_gen); }
int dab_filltree(Test **test,int irun) { int size = (ntuple == 0) ? 32 : ntuple; uint target = sizeof(targetData)/sizeof(double); int startVal = (size / 2) - 1; double *array = (double *) malloc(sizeof(double) * size); double *counts, *expected; int i, j; double x; uint start = 0; uint end = 0; uint rotAmount = 0; double *positionCounts; counts = (double *) malloc(sizeof(double) * target); expected = (double *) malloc(sizeof(double) * target); memset(counts, 0, sizeof(double) * target); positionCounts = (double *) malloc(sizeof(double) * size/2); memset(positionCounts, 0, sizeof(double) * size/2); test[0]->ntuple = size; test[1]->ntuple = size; /* Calculate expected counts. */ for (i = 0; i < target; i++) { expected[i] = targetData[i] * test[0]->tsamples; if (expected[i] < 4) { if (end == 0) start = i; } else if (expected[i] > 4) end = i; } start++; for (j = 0; j < test[0]->tsamples; j++) { int ret; memset(array, 0, sizeof(double) * size); i = 0; do { uint v = gsl_rng_get(rng); x = ((double) RotL(v, rotAmount)) / rmax_mask; i++; if (i > size * 2) { test[0]->pvalues[irun] = 0; return(0); } ret = insert(x, array, startVal); } while (ret == -1); positionCounts[ret/2]++; counts[i-1]++; if (j % (test[0]->tsamples/CYCLES) == 0) rotAmount++; } test[0]->pvalues[irun] = chisq_pearson(counts + start, expected + start, end - start); for (i = 0; i < size/2; i++) expected[i] = test[0]->tsamples/(size/2); test[1]->pvalues[irun] = chisq_pearson(positionCounts, expected, size/2); nullfree(positionCounts); nullfree(expected); nullfree(counts); nullfree(array); return(0); }
void diehard_dna(Test **test, int irun) { uint i,j,k,l,m,n,o,p,q,r,t,boffset; uint i0,j0,k0,l0,m0,n0,o0,p0,q0,r0; Xtest ptest; char **********w; /* * p = 141909, with sigma 339, FOR tsamples 2^21+1 2 letter words. * These cannot be varied unless one figures out the actual * expected "missing works" count as a function of sample size. SO: * * ptest.x = number of "missing words" given 2^21+1 trials * ptest.y = 141909 * ptest.sigma = 339 */ ptest.x = 0.0; /* Initial value */ ptest.y = 141909.0; ptest.sigma = 339.0; /* * We now make tsamples measurements, as usual, to generate the missing * statistic. Wow! 10 dimensions! I don't think even my tensor() * package will do that...;-) */ w = (char **********)malloc(4*sizeof(char *********)); for(i=0;i<4;i++){ w[i] = (char *********)malloc(4*sizeof(char ********)); for(j=0;j<4;j++){ w[i][j] = (char ********)malloc(4*sizeof(char *******)); for(k=0;k<4;k++){ w[i][j][k] = (char *******)malloc(4*sizeof(char******)); for(l=0;l<4;l++){ w[i][j][k][l] = (char ******)malloc(4*sizeof(char*****)); for(m=0;m<4;m++){ w[i][j][k][l][m] = (char *****)malloc(4*sizeof(char****)); for(n=0;n<4;n++){ w[i][j][k][l][m][n] = (char ****)malloc(4*sizeof(char***)); for(o=0;o<4;o++){ w[i][j][k][l][m][n][o] = (char ***)malloc(4*sizeof(char**)); for(p=0;p<4;p++){ w[i][j][k][l][m][n][o][p] = (char **)malloc(4*sizeof(char*)); for(q=0;q<4;q++){ w[i][j][k][l][m][n][o][p][q] = (char *)malloc(4*sizeof(char)); /* Zero the column */ memset(w[i][j][k][l][m][n][o][p][q],0,4*sizeof(char)); } } } } } } } } } /* * To minimize the number of rng calls, we use each j and k mod 32 * to determine the offset of the 10-bit long string (with * periodic wraparound) to be used for the next iteration. We * therefore have to "seed" the process with a random l */ q = gsl_rng_get(rng); for(t=0;t<test[0]->tsamples;t++){ if(overlap){ /* * Let's do this the cheap/easy way first, sliding a 20 bit * window along each int for the 32 possible starting * positions a la birthdays, before trying to slide it all * the way down the whole random bitstring implicit in a * long sequence of random ints. That way we can exit * the tsamples loop at tsamples = 2^15... */ if(t%32 == 0) { i0 = gsl_rng_get(rng); j0 = gsl_rng_get(rng); k0 = gsl_rng_get(rng); l0 = gsl_rng_get(rng); m0 = gsl_rng_get(rng); n0 = gsl_rng_get(rng); o0 = gsl_rng_get(rng); p0 = gsl_rng_get(rng); q0 = gsl_rng_get(rng); r0 = gsl_rng_get(rng); boffset = 0; } /* * Get four "letters" (indices into w) */ i = get_bit_ntuple(&i0,1,2,boffset); j = get_bit_ntuple(&j0,1,2,boffset); k = get_bit_ntuple(&k0,1,2,boffset); l = get_bit_ntuple(&l0,1,2,boffset); m = get_bit_ntuple(&m0,1,2,boffset); n = get_bit_ntuple(&n0,1,2,boffset); o = get_bit_ntuple(&o0,1,2,boffset); p = get_bit_ntuple(&p0,1,2,boffset); q = get_bit_ntuple(&q0,1,2,boffset); r = get_bit_ntuple(&r0,1,2,boffset); /* printf("%u: %u %u %u %u %u\n",t,i,j,k,l,boffset); */ w[i][j][k][l][m][n][o][p][q][r]++; boffset++; } else { /* * Get two "letters" (indices into w) */ boffset = q%32; i = gsl_rng_get(rng); i = get_bit_ntuple(&i,1,2,boffset); boffset = i%32; j = gsl_rng_get(rng); j = get_bit_ntuple(&j,1,2,boffset); boffset = j%32; k = gsl_rng_get(rng); k = get_bit_ntuple(&k,1,2,boffset); boffset = k%32; l = gsl_rng_get(rng); l = get_bit_ntuple(&l,1,2,boffset); boffset = l%32; m = gsl_rng_get(rng); m = get_bit_ntuple(&m,1,2,boffset); boffset = m%32; n = gsl_rng_get(rng); n = get_bit_ntuple(&n,1,2,boffset); boffset = n%32; o = gsl_rng_get(rng); o = get_bit_ntuple(&o,1,2,boffset); boffset = o%32; p = gsl_rng_get(rng); p = get_bit_ntuple(&p,1,2,boffset); boffset = p%32; q = gsl_rng_get(rng); q = get_bit_ntuple(&q,1,2,boffset); boffset = q%32; r = gsl_rng_get(rng); r = get_bit_ntuple(&r,1,2,boffset); w[i][j][k][l][m][n][o][p][q][r]++; } } /* * Now we count the holes, so to speak */ ptest.x = 0; for(i=0;i<4;i++){ for(j=0;j<4;j++){ for(k=0;k<4;k++){ for(l=0;l<4;l++){ for(m=0;m<4;m++){ for(n=0;n<4;n++){ for(o=0;o<4;o++){ for(p=0;p<4;p++){ for(q=0;q<4;q++){ for(r=0;r<4;r++){ if(w[i][j][k][l][m][n][o][p][q][r] == 0){ ptest.x += 1.0; /* printf("ptest.x = %f Hole: w[%u][%u][%u][%u] = %u\n",ptest.x,i,j,k,l,w[i][j][k][l]); */ } } } } } } } } } } } MYDEBUG(D_DIEHARD_DNA) { printf("%f %f %f\n",ptest.y,ptest.x,ptest.x-ptest.y); } Xtest_eval(&ptest); test[0]->pvalues[irun] = ptest.pvalue; MYDEBUG(D_DIEHARD_DNA) { printf("# diehard_dna(): test[0]->pvalues[%u] = %10.5f\n",irun,test[0]->pvalues[irun]); } /* * Don't forget to free w when done, in reverse order */ for(i=0;i<4;i++){ for(j=0;j<4;j++){ for(k=0;k<4;k++){ for(l=0;l<4;l++){ for(m=0;m<4;m++){ for(n=0;n<4;n++){ for(o=0;o<4;o++){ for(p=0;p<4;p++){ for(q=0;q<4;q++){ free(w[i][j][k][l][m][n][o][p][q]); } free(w[i][j][k][l][m][n][o][p]); } free(w[i][j][k][l][m][n][o]); } free(w[i][j][k][l][m][n]); } free(w[i][j][k][l][m]); } free(w[i][j][k][l]); } free(w[i][j][k]); } free(w[i][j]); } free(w[i]); } free(w); }
void diehard_oqso(Test **test, int irun) { uint i,j,k,l,i0,j0,k0,l0,t,boffset; Xtest ptest; char ****w; /* * p = 141909, with sigma 295, FOR tsamples 2^21 2 letter words. * These cannot be varied unless one figures out the actual * expected "missing works" count as a function of sample size. SO: * * ptest.x = number of "missing words" given 2^21 trials * ptest.y = 141909 * ptest.sigma = 295 */ ptest.x = 0.0; /* Initial value */ ptest.y = 141909.0; ptest.sigma = 295.0; /* * We now make tsamples measurements, as usual, to generate the * missing statistic. We proceed exactly as we did in opso, but * with a 4d 32x32x32x32 matrix and 5 bit indices. This should * basically be strongly related to a Knuth hyperplane test in * four dimensions. Equally obviously there is a sequence of * tests, all basically identical, that can be done here much * as rgb_bitdist tries to do them. I'll postpone thinking about * this in detail until I'm done with diehard and some more of STS * and maybe have implemented the REAL Knuth tests from the Art of * Programming. */ w = (char ****)malloc(32*sizeof(char ***)); for(i=0;i<32;i++){ w[i] = (char ***)malloc(32*sizeof(char **)); for(j=0;j<32;j++){ w[i][j] = (char **)malloc(32*sizeof(char *)); for(k=0;k<32;k++){ w[i][j][k] = (char *)malloc(32*sizeof(char)); /* Zero the column */ memset(w[i][j][k],0,32*sizeof(char)); } } } /* * To minimize the number of rng calls, we use each j and k mod 32 * to determine the offset of the 10-bit long string (with * periodic wraparound) to be used for the next iteration. We * therefore have to "seed" the process with a random l */ l = gsl_rng_get(rng); for(t=0;t<test[0]->tsamples;t++){ if(overlap){ /* * Let's do this the cheap/easy way first, sliding a 20 bit * window along each int for the 32 possible starting * positions a la birthdays, before trying to slide it all * the way down the whole random bitstring implicit in a * long sequence of random ints. That way we can exit * the tsamples loop at tsamples = 2^15... */ if(t%32 == 0) { i0 = gsl_rng_get(rng); j0 = gsl_rng_get(rng); k0 = gsl_rng_get(rng); l0 = gsl_rng_get(rng); boffset = 0; } /* * Get four "letters" (indices into w) */ i = get_bit_ntuple(&i0,1,5,boffset); j = get_bit_ntuple(&j0,1,5,boffset); k = get_bit_ntuple(&k0,1,5,boffset); l = get_bit_ntuple(&l0,1,5,boffset); /* printf("%u: %u %u %u %u %u\n",t,i,j,k,l,boffset); */ w[i][j][k][l]++; boffset++; } else { /* * Get four "letters" (indices into w) */ boffset = l%32; i = gsl_rng_get(rng); i = get_bit_ntuple(&i,1,5,boffset); boffset = i%32; j = gsl_rng_get(rng); j = get_bit_ntuple(&j,1,5,boffset); boffset = j%32; k = gsl_rng_get(rng); k = get_bit_ntuple(&k,1,5,boffset); boffset = k%32; l = gsl_rng_get(rng); l = get_bit_ntuple(&l,1,5,boffset); w[i][j][k][l]++; } } /* * Now we count the holes, so to speak */ ptest.x = 0.0; for(i=0;i<32;i++){ for(j=0;j<32;j++){ for(k=0;k<32;k++){ for(l=0;l<32;l++){ if(w[i][j][k][l] == 0){ ptest.x += 1.0; /* printf("ptest.x = %f Hole: w[%u][%u][%u][%u] = %u\n",ptest.x,i,j,k,l,w[i][j][k][l]); */ } } } } } MYDEBUG(D_DIEHARD_OQSO){ printf("%f %f %f\n",ptest.y,ptest.x,ptest.x-ptest.y); } Xtest_eval(&ptest); test[0]->pvalues[irun] = ptest.pvalue; MYDEBUG(D_DIEHARD_OQSO) { printf("# diehard_oqso(): ks_pvalue[%u] = %10.5f\n",irun,test[0]->pvalues[irun]); } /* * Don't forget to free w when done, in reverse order */ for(i=0;i<32;i++){ for(j=0;j<32;j++){ for(k=0;k<32;k++){ free(w[i][j][k]); } free(w[i][j]); } free(w[i]); } free(w); }