Exemplo n.º 1
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;
}
Exemplo n.º 2
0
static VALUE r_mpfr_get_z_2exp(VALUE self)
{
  VALUE ptr_return;
  MPFR *ptr_self;
  MP_INT *ptr_mpz;
  long int exp;
  r_mpfr_get_struct(ptr_self, self);
  mpz_make_struct_init(ptr_return, ptr_mpz);
  exp = mpfr_get_z_2exp(ptr_mpz, ptr_self);
  return rb_ary_new3(2, ptr_return, INT2NUM(exp));
}
Exemplo n.º 3
0
static VALUE r_mpfr_to_mpz(int argc, VALUE *argv, VALUE self)
{
  VALUE ptr_return;
  MPFR *ptr_self;
  MP_INT *ptr_mpz;
  mp_rnd_t rnd;
  rnd = r_mpfr_rnd_from_optional_argument(0, 1, argc, argv);
  r_mpfr_get_struct(ptr_self, self);
  mpz_make_struct_init(ptr_return, ptr_mpz);
  mpfr_get_z(ptr_mpz, ptr_self, rnd);
  return ptr_return;
}
Exemplo n.º 4
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;
}