gmp 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +4 -0
- data/ext/gmprandstate.c +40 -3
- data/ext/ruby_gmp.h +1 -0
- data/manual.tex +2 -2
- data/test/tc_random.rb +19 -0
- metadata +2 -2
data/CHANGELOG
CHANGED
data/ext/gmprandstate.c
CHANGED
@@ -90,7 +90,7 @@ VALUE r_gmprandstatesg_new(int argc, VALUE *argv, VALUE klass)
|
|
90
90
|
rb_raise(rb_eArgError, "size must be within [0..128]");
|
91
91
|
int rv = gmp_randinit_lc_2exp_size(rs_val, size_val);
|
92
92
|
if (rv == 0)
|
93
|
-
rb_raise(rb_eArgError, "could not gmp_randinit_lc_2exp_size with %
|
93
|
+
rb_raise(rb_eArgError, "could not gmp_randinit_lc_2exp_size with %lu", size_val);
|
94
94
|
}
|
95
95
|
|
96
96
|
if (free_a_val) { mpz_temp_free(a_val); }
|
@@ -193,7 +193,7 @@ VALUE r_gmprandstate_urandomb(VALUE self, VALUE arg)
|
|
193
193
|
|
194
194
|
mprandstate_get_struct(self,self_val);
|
195
195
|
|
196
|
-
|
196
|
+
if (FIXNUM_P(arg)) {
|
197
197
|
mpz_make_struct_init(res, res_val);
|
198
198
|
mpz_urandomb(res_val, self_val, FIX2INT(arg));
|
199
199
|
} else {
|
@@ -203,6 +203,43 @@ VALUE r_gmprandstate_urandomb(VALUE self, VALUE arg)
|
|
203
203
|
return res;
|
204
204
|
}
|
205
205
|
|
206
|
+
/*
|
207
|
+
* call-seq:
|
208
|
+
* rand_state.urandomm(fixnum)
|
209
|
+
*
|
210
|
+
* From the GMP Manual:
|
211
|
+
*
|
212
|
+
* Generate a uniformly distributed random integer in the range 0 to
|
213
|
+
* fixnum-1, inclusive.
|
214
|
+
*/
|
215
|
+
VALUE r_gmprandstate_urandomm(VALUE self, VALUE arg)
|
216
|
+
{
|
217
|
+
MP_RANDSTATE *self_val;
|
218
|
+
MP_INT *res_val, *arg_val;
|
219
|
+
int free_arg_val = 0;
|
220
|
+
VALUE res;
|
221
|
+
|
222
|
+
mprandstate_get_struct(self,self_val);
|
223
|
+
|
224
|
+
if (GMPZ_P(arg)) {
|
225
|
+
mpz_get_struct(arg, arg_val);
|
226
|
+
} else if (FIXNUM_P(arg)) {
|
227
|
+
mpz_temp_alloc(arg_val);
|
228
|
+
mpz_init_set_ui(arg_val, FIX2INT(arg));
|
229
|
+
free_arg_val = 1;
|
230
|
+
} else if (BIGNUM_P(arg)) {
|
231
|
+
mpz_temp_from_bignum(arg_val, arg);
|
232
|
+
free_arg_val = 1;
|
233
|
+
} else {
|
234
|
+
typeerror_as(ZXB, "arg");
|
235
|
+
}
|
236
|
+
|
237
|
+
mpz_make_struct_init(res, res_val);
|
238
|
+
mpz_urandomm(res_val, self_val, arg_val);
|
239
|
+
if (free_arg_val) { mpz_temp_free(arg_val); }
|
240
|
+
|
241
|
+
return res;
|
242
|
+
}
|
206
243
|
|
207
244
|
void init_gmprandstate()
|
208
245
|
{
|
@@ -220,5 +257,5 @@ void init_gmprandstate()
|
|
220
257
|
|
221
258
|
// Integer Random Numbers
|
222
259
|
rb_define_method(cGMP_RandState, "urandomb", r_gmprandstate_urandomb, 1);
|
223
|
-
|
260
|
+
rb_define_method(cGMP_RandState, "urandomm", r_gmprandstate_urandomm, 1);
|
224
261
|
}
|
data/ext/ruby_gmp.h
CHANGED
@@ -224,6 +224,7 @@ extern VALUE r_gmprandstate_seed(VALUE self, VALUE arg);
|
|
224
224
|
|
225
225
|
// Integer Random Numbers
|
226
226
|
extern VALUE r_gmprandstate_urandomb(VALUE self, VALUE arg);
|
227
|
+
extern VALUE r_gmprandstate_urandomm(VALUE self, VALUE arg);
|
227
228
|
|
228
229
|
|
229
230
|
/* from gmpbench_timing.c */
|
data/manual.tex
CHANGED
@@ -28,8 +28,8 @@
|
|
28
28
|
\huge{gmp} &\\
|
29
29
|
\midrule[3pt]
|
30
30
|
\multicolumn{2}{r}{\large{Ruby bindings to the GMP library}}\\
|
31
|
-
\multicolumn{2}{r}{\large{Edition 0.4.
|
32
|
-
\multicolumn{2}{r}{\large{
|
31
|
+
\multicolumn{2}{r}{\large{Edition 0.4.1}}\\
|
32
|
+
\multicolumn{2}{r}{\large{1 March 2010}}
|
33
33
|
\end{tabular}
|
34
34
|
|
35
35
|
\vfill
|
data/test/tc_random.rb
CHANGED
@@ -22,6 +22,25 @@ class TC_Random < Test::Unit::TestCase
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
def test_urandomm
|
26
|
+
@a = GMP::RandState.new
|
27
|
+
@a.seed(8675309)
|
28
|
+
g1 = [21, 849, 6428, 7329, 288845, 7712337, 9, 613, 5710, 3880, 246305, 9623186,
|
29
|
+
22, 47, 5055, 5189, 32571, 2585012, 19, 415, 4940, 3658, 248890, 2180120]
|
30
|
+
n1 = [32, 1024, 6563, 11213, 362880, 9699690]
|
31
|
+
g1.size.times do |i|
|
32
|
+
assert_equal(g1[i], @a.urandomm(n1[i%6]), "GMP::RandState should urandomm predictably.")
|
33
|
+
end
|
34
|
+
|
35
|
+
@b = GMP::RandState.new
|
36
|
+
@b.seed(67898771)
|
37
|
+
g2 = [ 1, 2, 3, 182, 393, 1232, 238, 10168, 10671, 28310,
|
38
|
+
72259, 70015, 27964, 363191, 371324, 66644, 188620, 1241121, 263944, 1357676]
|
39
|
+
g2.size.times do |i|
|
40
|
+
assert_equal(g2[i], @b.urandomm(GMP::Z(i**5+5)), "GMP::RandState should urandomb predictably.")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
25
44
|
def test_reseed
|
26
45
|
@c = GMP::RandState.new
|
27
46
|
@c.seed(1000)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gmp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomasz Wegrzanowski
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2010-01
|
13
|
+
date: 2010-03-01 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|