Пример #1
0
/* Initialize random number generator.  Note that some bytes of the "noise"
 * string will have random junk in them.  This is intentional. */
void init_rng() {
        int a = 0;
        uint8_t *noise = 0;
        int64_t tstamp = 0;
        pid_t pnum = 1;

        noise = (uint8_t *)dw_malloc(512);
        if(noise == 0) {
                dw_fatal("error allocating memory for noise");
        }
#ifdef VALGRIND_NOERRORS
        /* Valgrind reports our intentional use of values of uncleared
         * allocated memory as one source of entropy as an error, so we
         * allow it to be disabled for Valgrind testing */
        memset(noise,0,512);
#endif /* VALGRIND_NOERRORS */

        get_entropy_from_seedfile(noise,256);

        /* Get entropy from the current timestamp */
        set_time();
        tstamp = get_time();
        for(a = 0 ; a < 8 ; a++) {
                *(noise + a + 256) = tstamp & 0xff;
                tstamp >>= 8;
        }

        /* Get entropy from the process' ID number */
        pnum = getpid();
        for(a = 0 ; a < sizeof(pnum) ; a++ ) {
                *(noise + a + 272) = pnum & 0xff;
                pnum >>= 8;
        }

        /* Initialize the RNG based on the contents of noise */
        noise_to_rng(noise,510);

        if(noise != 0) {
                free(noise);
                noise = 0;
        }

}
Пример #2
0
TElliptical* CreateEllipticalFromPosterior_Step(TVector R, int dim, TVector center, TMatrix scale, PRECISION p_lo, PRECISION p_hi)
{
  TElliptical *elliptical=(TElliptical*)NULL;
  PRECISION *table, inc, x, y;
  int i, j, k, m=30;

  if (dim > 0)
    {
      table=(PRECISION*)dw_malloc((m+1)*sizeof(PRECISION));

      SortVectorAscending(R,R);

      for (k=m; k >= 1; k--)
	{
	  inc=(p_hi-p_lo)/(PRECISION)k;
	  for (x=p_lo, j=0; j <= k; x+=inc, j++)
	    {
	      i=floor(y=x*(PRECISION)(DimV(R)-1));
	      if (i >= DimV(R)-1)
		table[j]=ElementV(R,DimV(R)-1);
	      else
		table[j]=(1.0-y+i)*ElementV(R,i) + (y-i)*ElementV(R,i+1);
	      //table[j]=(i > 0) ? 0.5*(ElementV(R,i-1) + ElementV(R,i)) : ElementV(R,i);
	    }
	  for (j=1; j <= k; j++)
	    if (table[j] <= table[j-1]) break;
	  if (j > k) break;
	}

      if (k <= 0)
	{
	  dw_free(table);
	  printf("Not enough variation in the posterior draws to form proposal\n");
	  dw_exit(0);
	}

      elliptical=CreateElliptical_Step(dim,center,scale,table,k);
      dw_free(table);
    }

  return elliptical;
}