summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-06 07:18:33 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-06 07:18:33 (GMT)
commita331eeb529c314af0bffc94e9ec84ad20486a0f1 (patch)
tree00aa66aee6b57312650d936f7ee0681b4f090c3f /random.c
parent3377db6b32665b0e1eb5fa19651f4e3e2272282f (diff)
random.c: refactor
* random.c (rand_random, rb_f_rand): refactor, split condition expressions, and return in the precedence. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54931 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c66
1 files changed, 24 insertions, 42 deletions
diff --git a/random.c b/random.c
index 17aa532..bfc1feb 100644
--- a/random.c
+++ b/random.c
@@ -1281,45 +1281,29 @@ static VALUE
rand_random(int argc, VALUE *argv, VALUE obj, rb_random_t *rnd)
{
VALUE vmax, v;
- double max = 0.0;
- if (argc == 0) {
- goto float_rand;
- }
- else {
- rb_check_arity(argc, 0, 1);
+ if (rb_check_arity(argc, 0, 1) == 0) {
+ return rb_float_new(random_real(obj, rnd, TRUE));
}
vmax = argv[0];
- if (NIL_P(vmax)) {
- v = Qnil;
+ if (NIL_P(vmax)) return Qnil;
+ if (!RB_TYPE_P(vmax, T_FLOAT)) {
+ v = rb_check_to_int(vmax);
+ if (!NIL_P(v)) return rand_int(obj, rnd, v, 1);
}
- else if (!RB_TYPE_P(vmax, T_FLOAT) && (v = rb_check_to_int(vmax), !NIL_P(v))) {
- v = rand_int(obj, rnd, v, 1);
- }
- else if (v = rb_check_to_float(vmax), !NIL_P(v)) {
- max = float_value(v);
+ v = rb_check_to_float(vmax);
+ if (!NIL_P(v)) {
+ const double max = float_value(v);
if (max < 0.0) {
- v = Qnil;
+ return Qnil;
}
else {
- uint32_t a, b;
- double r;
-
- float_rand:
- a = random_int32(obj, rnd);
- b = random_int32(obj, rnd);
- r = int_pair_to_real_exclusive(a, b);
- if (max > 0.0) r *= max;;
- v = rb_float_new(r);
+ double r = random_real(obj, rnd, TRUE);
+ if (max > 0.0) r *= max;
+ return rb_float_new(r);
}
}
- else if ((v = rand_range(obj, rnd, vmax)) != Qfalse) {
- /* nothing to do */
- }
- else {
- return Qfalse;
- }
- return v;
+ return rand_range(obj, rnd, vmax);
}
static VALUE
@@ -1404,21 +1388,19 @@ random_equal(VALUE self, VALUE other)
static VALUE
rb_f_rand(int argc, VALUE *argv, VALUE obj)
{
- VALUE v, vmax, r;
+ VALUE vmax;
rb_random_t *rnd = rand_start(&default_rand);
- if (rb_check_arity(argc, 0, 1) == 0) goto zero_arg;
- vmax = argv[0];
- if (NIL_P(vmax)) goto zero_arg;
- if ((v = rand_range(Qnil, rnd, vmax)) != Qfalse) {
- return v;
- }
- vmax = rb_to_int(vmax);
- if (vmax == INT2FIX(0) || NIL_P(r = rand_int(Qnil, rnd, vmax, 0))) {
- zero_arg:
- return DBL2NUM(genrand_real(&rnd->mt));
+ if (rb_check_arity(argc, 0, 1) && !NIL_P(vmax = argv[0])) {
+ VALUE v = rand_range(Qnil, rnd, vmax);
+ if (v != Qfalse) return v;
+ vmax = rb_to_int(vmax);
+ if (vmax != INT2FIX(0)) {
+ v = rand_int(Qnil, rnd, vmax, 0);
+ if (!NIL_P(v)) return v;
+ }
}
- return r;
+ return DBL2NUM(genrand_real(&rnd->mt));
}
/*