Beispiel #1
0
/*
 * call-seq:
 *   rand_state.mpfr_urandomb()
 *
 * From the MPFR Manual:
 *
 * Generate a uniformly distributed random float in the interval 0 <= rop < 1.
 */
VALUE r_gmprandstate_mpfr_urandomb(int argc, VALUE *argv, VALUE self)
{
  MP_RANDSTATE *self_val;
  MP_FLOAT *res_val;
  VALUE res;
  unsigned long prec = 0;
  
  if (argc > 1)
    rb_raise (rb_eArgError, "wrong # of arguments(%d for 0 or 1)", argc);

  mprandstate_get_struct (self,self_val);
  
  if (argc == 1) {
    if (FIXNUM_P (argv[0])) {
      if (FIX2INT (argv[0]) >= 0)
        prec = FIX2INT (argv[0]);
      else
        rb_raise (rb_eRangeError, "prec must be non-negative");
    } else {
      rb_raise (rb_eTypeError, "prec must be a Fixnum");
    }
  }
  
  mpf_make_struct (res, res_val);
  if (prec == 0) { mpf_init (res_val); }
  else           { mpf_init2 (res_val, prec); }
  
  mpfr_urandomb (res_val, self_val);
  
  return res;
}
Beispiel #2
0
/*
 * call-seq:
 *   rand_state.urandomm(integer)
 *
 * From the GMP Manual:
 *
 * Generate a uniformly distributed random integer in the range 0 to
 * _integer-1_, inclusive. _integer_ can be an instance of GMP::Z,
 *  Fixnum, or Bignum
 */
VALUE r_gmprandstate_urandomm(VALUE self, VALUE arg)
{
  MP_RANDSTATE *self_val;
  MP_INT *res_val, *arg_val;
  int free_arg_val = 0;
  VALUE res;

  mprandstate_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_get_struct(arg, arg_val);
  } else if (FIXNUM_P(arg)) {
    mpz_temp_alloc(arg_val);
    mpz_init_set_ui(arg_val, FIX2INT(arg));
    free_arg_val = 1;
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val, arg);
    free_arg_val = 1;
  } else {
    typeerror_as(ZXB, "arg");
  }

  mpz_make_struct_init(res, res_val);
  mpz_urandomm(res_val, self_val, arg_val);
  if (free_arg_val) { mpz_temp_free(arg_val); }

  return res;
}
Beispiel #3
0
VALUE r_gmprandstate_initialize(int argc, VALUE *argv, VALUE self)
{
  MP_RANDSTATE *self_val;
  (void)argv;

  if (argc != 0) {
    mprandstate_get_struct(self,self_val);
  }
  return Qnil;
}
Beispiel #4
0
/* Return a random number of MPFR object. */
static VALUE r_gmprandstate_mpfr_urandomb2(int argc, VALUE *argv, VALUE self)
{
  MP_RANDSTATE *ptr_self;
  MPFR *ptr_return;
  VALUE val_ret;
  mp_prec_t prec;
  prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
  mprandstate_get_struct(self, ptr_self);
  r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
  mpfr_urandomb(ptr_return, ptr_self);
  return val_ret;
}
Beispiel #5
0
/*
 * call-seq:
 *   rand_state.rrandomb(fixnum)
 *
 * From the GMP Manual:
 *
 * Generate a random integer with long strings of zeros and ones in the binary
 * representation. Useful for testing functions and algorithms, since this kind
 * of random numbers have proven to be more likely to trigger corner-case bugs.
 * The random number will be in the range 0 to 2^n-1, inclusive. 
 */
VALUE r_gmprandstate_rrandomb(VALUE self, VALUE arg)
{
  MP_RANDSTATE *self_val;
  MP_INT *res_val;
  VALUE res;

  mprandstate_get_struct(self,self_val);

  if (FIXNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_rrandomb(res_val, self_val, FIX2INT(arg));
  } else {
    typeerror(X);
  }

  return res;
}
Beispiel #6
0
/*
 * call-seq:
 *   rand_state.seed(integer)
 *
 * From the GMP Manual:
 *
 * Set an initial seed value into state.
 *
 * The size of a seed determines how many different sequences of random numbers
 * that it's possible to generate. The “quality” of the seed is the randomness
 * of a given seed compared to the previous seed used, and this affects the
 * randomness of separate number sequences. The method for choosing a seed is
 * critical if the generated numbers are to be used for important applications,
 * such as generating cryptographic keys.
 *
 * Traditionally the system time has been used to seed, but care needs to be
 * taken with this. If an application seeds often and the resolution of the
 * system clock is low, then the same sequence of numbers might be repeated.
 * Also, the system time is quite easy to guess, so if unpredictability is
 * required then it should definitely not be the only source for the seed
 * value. On some systems there's a special device /dev/random which provides
 * random data better suited for use as a seed.
 */
VALUE r_gmprandstate_seed(VALUE self, VALUE arg)
{
  MP_RANDSTATE *self_val;
  MP_INT *arg_val;

  mprandstate_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_get_struct(arg,arg_val);
    gmp_randseed(self_val, arg_val);
  } else if (FIXNUM_P(arg)) {
    gmp_randseed_ui(self_val, FIX2INT(arg));
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val, arg);
    gmp_randseed(self_val, arg_val);
  } else {
    typeerror(ZXB);
  }
  return arg;
}