summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
Diffstat (limited to 'random.c')
-rw-r--r--random.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/random.c b/random.c
index 9405ff1917..616cd68b52 100644
--- a/random.c
+++ b/random.c
@@ -322,7 +322,6 @@ int_pair_to_real_inclusive(unsigned int a, unsigned int b)
}
VALUE rb_cRandom;
-static VALUE rb_Random_DEFAULT;
#define id_minus '-'
#define id_plus '+'
static ID id_rand, id_bytes;
@@ -1124,6 +1123,8 @@ rand_range(struct MT* mt, VALUE range)
return v;
}
+static VALUE rand_random(int argc, VALUE *argv, rb_random_t *rnd);
+
/*
* call-seq:
* prng.rand -> float
@@ -1156,7 +1157,12 @@ rand_range(struct MT* mt, VALUE range)
static VALUE
random_rand(int argc, VALUE *argv, VALUE obj)
{
- rb_random_t *rnd = get_rnd(obj);
+ return rand_random(argc, argv, get_rnd(obj));
+}
+
+static VALUE
+rand_random(int argc, VALUE *argv, rb_random_t *rnd)
+{
VALUE vmax, v;
if (argc == 0) {
@@ -1293,8 +1299,7 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
static VALUE
random_s_rand(int argc, VALUE *argv, VALUE obj)
{
- rand_start(&default_rand);
- return random_rand(argc, argv, rb_Random_DEFAULT);
+ return rand_random(argc, argv, rand_start(&default_rand));
}
static st_index_t hashseed;
@@ -1404,9 +1409,11 @@ Init_Random(void)
rb_define_private_method(rb_cRandom, "left", random_left, 0);
rb_define_method(rb_cRandom, "==", random_equal, 1);
- rb_Random_DEFAULT = TypedData_Wrap_Struct(rb_cRandom, &random_data_type, &default_rand);
- rb_global_variable(&rb_Random_DEFAULT);
- rb_define_const(rb_cRandom, "DEFAULT", rb_Random_DEFAULT);
+ {
+ VALUE rand_default = TypedData_Wrap_Struct(rb_cRandom, &random_data_type, &default_rand);
+ rb_gc_register_mark_object(rand_default);
+ rb_define_const(rb_cRandom, "DEFAULT", rand_default);
+ }
rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1);
rb_define_singleton_method(rb_cRandom, "rand", random_s_rand, -1);