static mrb_value mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary) { mrb_int i; mrb_value random = mrb_nil_value(); if (RARRAY_LEN(ary) > 1) { mrb_get_args(mrb, "|o", &random); if( mrb_nil_p(random) ) { mrb_random_g_rand_seed(mrb); } else { mrb_data_check_type(mrb, random, &mt_state_type); mrb_random_rand_seed(mrb, random); } mrb_ary_modify(mrb, mrb_ary_ptr(ary)); for (i = RARRAY_LEN(ary) - 1; i > 0; i--) { mrb_int j; if( mrb_nil_p(random) ) { j = mrb_fixnum(mrb_random_mt_g_rand(mrb, mrb_fixnum_value(RARRAY_LEN(ary)))); } else { j = mrb_fixnum(mrb_random_mt_rand(mrb, DATA_PTR(random), mrb_fixnum_value(RARRAY_LEN(ary)))); } mrb_value t = RARRAY_PTR(ary)[i]; RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j]; RARRAY_PTR(ary)[j] = t; } } return ary; }
static mrb_value mrb_random_g_rand(mrb_state *mrb, mrb_value self) { mrb_value max; max = get_opt(mrb); mrb_random_g_rand_seed(mrb); return mrb_random_mt_g_rand(mrb, max); }
static mrb_value mrb_random_g_rand(mrb_state *mrb, mrb_value self) { mrb_value max; mrb_value seed; max = get_opt(mrb); seed = mrb_gv_get(mrb, mrb_intern2(mrb, GLOBAL_RAND_SEED_KEY, GLOBAL_RAND_SEED_KEY_CSTR_LEN)); if (mrb_nil_p(seed)) { mrb_random_mt_g_srand(mrb, mrb_nil_value()); } return mrb_random_mt_g_rand(mrb, max); }
static mrb_value mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary) { mrb_int i; mrb_value seed; seed = mrb_gv_get(mrb, mrb_intern2(mrb, GLOBAL_RAND_SEED_KEY, GLOBAL_RAND_SEED_KEY_CSTR_LEN)); if (mrb_nil_p(seed)) { mrb_random_mt_g_srand(mrb, mrb_nil_value()); } if (RARRAY_LEN(ary) > 1) { mrb_ary_modify(mrb, mrb_ary_ptr(ary)); for (i = RARRAY_LEN(ary) - 1; i > 0; i--) { mrb_int j = mrb_fixnum(mrb_random_mt_g_rand(mrb, mrb_fixnum_value(RARRAY_LEN(ary)))); mrb_value t = RARRAY_PTR(ary)[i]; RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j]; RARRAY_PTR(ary)[j] = t; } } return ary; }