Exemplo n.º 1
0
/*Returns a uniform random float.
  The expected value is within FLT_MIN (e.g., 1E-37) of 0.5.
  _bits: An initial set of random bits.
  _base: This should be -(the number of bits in _bits), up to -64.
  Return: A float uniformly distributed between 0 (inclusive) and 1
           (exclusive).
          The average value was measured over 2**32 samples to be
           0.499991407275206357.*/
static float isaac64_float_bits(isaac64_ctx *_ctx,uint64_t _bits,int _base){
  float ret;
  int   nbits_needed;
  while(!_bits){
    if(_base+FLT_MANT_DIG<FLT_MIN_EXP)return 0;
    _base-=64;
    _bits=isaac64_next_uint64(_ctx);
  }
  nbits_needed=FLT_MANT_DIG-ilog_64_nz(_bits);
#if FLT_MANT_DIG>64
  ret=ldexpf((float)_bits,_base);
# if FLT_MANT_DIG>129
  while(64-nbits_needed<0){
# else
  if(64-nbits_needed<0){
# endif
    _base-=64;
    nbits_needed-=64;
    ret+=ldexpf((float)isaac64_next_uint64(_ctx),_base);
  }
  _bits=isaac64_next_uint64(_ctx)>>(64-nbits_needed);
  ret+=ldexpf((float)_bits,_base-nbits_needed);
#else
  if(nbits_needed>0){
    _bits=_bits<<nbits_needed|isaac64_next_uint64(_ctx)>>(64-nbits_needed);
  }
# if FLT_MANT_DIG<64
  else _bits>>=-nbits_needed;
Exemplo n.º 2
0
uint64_t isaac64_next_uint(isaac64_ctx *_ctx,uint64_t _n){
  uint64_t r;
  uint64_t v;
  uint64_t d;
  do{
    r=isaac64_next_uint64(_ctx);
    v=r%_n;
    d=r-v;
  }
  while(((d+_n-1)&ISAAC64_MASK)<d);
  return v;
}
Exemplo n.º 3
0
int main(int _argc,const char *_argv[]){
  isaac64_ctx isaac64;
  int         i;
  int         j;

  /*This is how many tests you plan to run.*/
  plan_tests(2);
  isaac64_init(&isaac64,NULL,0);
  for(j=0;j<ISAAC64_SZ;j++)isaac64_next_uint64(&isaac64);
  for(i=0;i<2;i++){
    int nmatches;
    nmatches=0;
    for(j=0;j<ISAAC64_SZ;j++){
      nmatches+=isaac64_next_uint64(&isaac64)==STATEVEC64[(i+1)*ISAAC64_SZ-j-1];
    }
    ok1(nmatches==ISAAC64_SZ);
  }
  /*TODO: We should test the random float/double routines, but they are not
     guaranteed to return the same values on all platforms, because the number
     of bits in the mantissa may be different.
    Perhaps some simple statistical tests would suffice.*/
  return exit_status();
}
Exemplo n.º 4
0
/* random_random return a double in the range [0, 1).
*/
static PyObject *
random_random(RandomObject *self)
{
#if 0
    double d =  isaac64_next_double(&self->context);
#else
    union {
        u64 i;
        double d;
    } x;
    x.i = isaac64_next_uint64(&self->context);
    DSFMT_INT64_TO_DOUBLE(x.i);
    return PyFloat_FromDouble(x.d - 1.0);
#endif
}