diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-07-17 04:56:12 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-07-17 04:56:12 +0000 |
commit | 115c220b4d7593bccff0ad3feb66bcdbc4bb008a (patch) | |
tree | 1b206ecf1cfa5996dfbac0700aca1387e9df75f5 | |
parent | bc0ea18dbd073bea10a8f3c610bb879a31468c60 (diff) | |
download | ruby-115c220b4d7593bccff0ad3feb66bcdbc4bb008a.tar.gz |
* random.c (random_equal): new method Random#==.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24162 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | random.c | 21 |
2 files changed, 25 insertions, 0 deletions
@@ -1,3 +1,7 @@ +Fri Jul 17 13:55:53 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * random.c (random_equal): new method Random#==. + Fri Jul 17 13:35:47 2009 Tanaka Akira <akr@fsij.org> * ext/socket/init.c (rsock_getfamily): return AF_UNSPEC if getsockname @@ -911,6 +911,26 @@ random_float(int argc, VALUE *argv, VALUE obj) } /* + * call-seq: + * prng1 == prng2 -> true or false + * + * Returns true if the generators' states equal. + */ +static VALUE +random_equal(VALUE self, VALUE other) +{ + rb_random_t *r1, *r2; + if (rb_obj_class(self) != rb_obj_class(other)) return Qfalse; + r1 = get_rnd(self); + r2 = get_rnd(other); + if (!RTEST(rb_funcall2(r1->seed, rb_intern("=="), 1, &r2->seed))) return Qfalse; + if (memcmp(r1->mt.state, r2->mt.state, sizeof(r1->mt.state))) return Qfalse; + if ((r1->mt.next - r1->mt.state) != (r2->mt.next - r2->mt.state)) return Qfalse; + if (r1->mt.left != r2->mt.left) return Qfalse; + return Qtrue; +} + +/* * call-seq: * rand(max=0) => number * @@ -991,6 +1011,7 @@ Init_Random(void) rb_define_method(rb_cRandom, "marshal_load", random_load, 1); rb_define_method(rb_cRandom, "state", random_state, 0); rb_define_method(rb_cRandom, "left", random_left, 0); + rb_define_method(rb_cRandom, "==", random_equal, 1); rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1); rb_define_singleton_method(rb_cRandom, "rand", rb_f_rand, -1); |