Class: GMP::RandState
- Inherits:
-
Object
- Object
- GMP::RandState
- Defined in:
- ext/gmp.c,
ext/gmprandstate.c
Class Method Summary collapse
-
.new(*args) ⇒ Object
Initializes a new Random State object.
Instance Method Summary collapse
- #initialize(*args) ⇒ Object constructor
-
#mpfr_urandom ⇒ Object
From the MPFR Manual:.
-
#mpfr_urandomb ⇒ Object
From the MPFR Manual:.
-
#rrandomb(Fixnum) ⇒ Object
From the GMP Manual:.
-
#seed(integer) ⇒ Object
From the GMP Manual:.
-
#urandomb(Fixnum) ⇒ Object
From the GMP Manual:.
-
#urandomm(integer) ⇒ Object
From the GMP Manual:.
Constructor Details
#initialize(*args) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'ext/gmprandstate.c', line 101
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;
}
|
Class Method Details
.GMP::RandState.new ⇒ Object .GMP::RandState.new(: mt) ⇒ Object .GMP::RandState.new(: lc_2exp, a, c, m2exp) ⇒ Object .GMP::RandState.new(: lc_2exp_size, size) ⇒ Object
Initializes a new Random State object. Multiple GMP::RandState objects can be instantiated. They may use different generators and the states are kept separate.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'ext/gmprandstate.c', line 40
VALUE r_gmprandstatesg_new(int argc, VALUE *argv, VALUE klass)
{
MP_RANDSTATE *rs_val;
VALUE rs;
VALUE algorithm, arg2, arg3, arg4;
ID algorithm_id = rb_intern("default");
MP_INT *a_val = NULL;
unsigned long c_val, m2exp_val;
unsigned long size_val;
int free_a_val = 0;
ID default_algorithm = rb_intern("default");
ID mt_algorithm = rb_intern("mt");
ID lc_2exp_algorithm = rb_intern("lc_2exp");
ID lc_2exp_size_algorithm = rb_intern("lc_2exp_size");
(void)klass;
mprandstate_make_struct(rs, rs_val);
rb_scan_args(argc, argv, "04", &algorithm, &arg2, &arg3, &arg4);
if (NIL_P(algorithm)) { algorithm_id = rb_intern("default"); } /* default value */
if (SYMBOL_P(algorithm)) { algorithm_id = rb_to_id(algorithm); }
if (algorithm_id == default_algorithm ||
algorithm_id == mt_algorithm) {
if (argc > 1)
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
gmp_randinit_default(rs_val);
} else if (algorithm_id == lc_2exp_algorithm) {
if (argc != 4)
rb_raise(rb_eArgError, "wrong number of arguments (%d for 4)", argc);
if (GMPZ_P(arg2)) {
mpz_get_struct(arg2, a_val);
} else if (FIXNUM_P(arg2)) {
mpz_temp_alloc(a_val);
mpz_init_set_ui(a_val, FIX2INT(arg2));
free_a_val = 1;
} else if (BIGNUM_P(arg2)) {
mpz_temp_from_bignum(a_val, arg2);
free_a_val = 1;
} else {
typeerror_as(ZXB, "b");
}
c_val = NUM2LONG(arg3);
m2exp_val = NUM2LONG(arg4);
gmp_randinit_lc_2exp(rs_val, a_val, c_val, m2exp_val);
} else if (algorithm_id == lc_2exp_size_algorithm) {
if (argc != 2)
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
size_val = NUM2LONG(arg2);
if (size_val > 128)
rb_raise(rb_eArgError, "size must be within [0..128]");
if (gmp_randinit_lc_2exp_size (rs_val, size_val) == 0)
rb_raise(rb_eArgError, "could not gmp_randinit_lc_2exp_size with %lu", size_val);
}
if (free_a_val) { mpz_temp_free(a_val); }
rb_obj_call_init(rs, argc, argv);
return rs;
}
|
Instance Method Details
#mpfr_urandom ⇒ Object
From the MPFR Manual:
> Generate a uniformly distributed random float. The floating-point number > ‘rop` can be seen as if a random real number is generated according to the > continuous uniform distribution on the interval [0, 1] and then rounded in > the direction `RNDN`.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'ext/gmprandstate.c', line 335
VALUE r_gmprandstate_mpfr_urandom(int argc, VALUE *argv, VALUE self_val)
{
MP_RANDSTATE *self;
MP_FLOAT *res;
mp_rnd_t rnd_mode;
VALUE res_val, prec_val, rnd_mode_val;
unsigned long int prec = 0;
mprandstate_get_struct (self_val, self);
rb_scan_args (argc, argv, "02", &rnd_mode_val, &prec_val);
if (NIL_P (rnd_mode_val)) { rnd_mode = __gmp_default_rounding_mode; }
else { rnd_mode = r_get_rounding_mode(rnd_mode_val); }
if (NIL_P (prec_val)) {
prec = mpfr_get_default_prec();
} else if (FIXNUM_P (prec_val)) {
if (FIX2INT (prec_val) < 2)
rb_raise (rb_eRangeError, "precision must be at least 2");
prec = FIX2INT (prec_val);
} else {
rb_raise (rb_eTypeError, "precision must be a Fixnum");
}
mpf_make_struct (res_val, res);
mpf_init2 (res, prec);
mpfr_urandom (res, self, rnd_mode);
return res_val;
}
|
#mpfr_urandomb ⇒ Object
From the MPFR Manual:
> Generate a uniformly distributed random float in the interval > _0 <= rop < 1_.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'ext/gmprandstate.c', line 289
VALUE r_gmprandstate_mpfr_urandomb(int argc, VALUE *argv, VALUE self_val)
{
MP_RANDSTATE *self;
MP_FLOAT *res;
VALUE res_val;
unsigned long prec = 0;
if (argc > 1)
rb_raise (rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
mprandstate_get_struct (self_val, self);
if (argc == 1) {
if (FIXNUM_P (argv[0])) {
if (FIX2INT (argv[0]) < 2)
rb_raise (rb_eRangeError, "precision must be at least 2");
prec = FIX2INT (argv[0]);
} else {
rb_raise (rb_eTypeError, "precision must be a Fixnum");
}
}
mpf_make_struct (res_val, res);
if (prec == 0) { mpf_init (res); }
else { mpf_init2 (res, prec); }
mpfr_urandomb (res, self);
return res_val;
}
|
#rrandomb(Fixnum) ⇒ Object
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.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'ext/gmprandstate.c', line 256
VALUE r_gmprandstate_rrandomb(VALUE self, VALUE arg)
{
MP_RANDSTATE *self_val;
MP_INT *res_val;
VALUE res = 0;
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;
}
|
#seed(integer) ⇒ Object
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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'ext/gmprandstate.c', line 152
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;
}
|
#urandomb(Fixnum) ⇒ Object
From the GMP Manual:
> Generate a uniformly distributed random integer in the range 0 to > _2^fixnum-1_, inclusive.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'ext/gmprandstate.c', line 187
VALUE r_gmprandstate_urandomb(VALUE self, VALUE arg)
{
MP_RANDSTATE *self_val;
MP_INT *res_val;
VALUE res = 0;
mprandstate_get_struct(self,self_val);
if (FIXNUM_P(arg)) {
mpz_make_struct_init(res, res_val);
mpz_urandomb(res_val, self_val, FIX2INT(arg));
} else {
typeerror(X);
}
return res;
}
|
#urandomm(integer) ⇒ Object
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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'ext/gmprandstate.c', line 215
VALUE r_gmprandstate_urandomm(VALUE self, VALUE arg)
{
MP_RANDSTATE *self_val;
MP_INT *res_val, *arg_val = NULL;
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;
}
|