summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-20 09:26:38 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-20 09:26:38 (GMT)
commit1cd9c3ce62aef3c45e72c002cbc17706ffca67c9 (patch)
treea9f478ef02c3292615b9267bd36cd168a3a81418 /random.c
parent74bf1767e3e0812c3f526a3bb0972e9145ed4461 (diff)
random.c: Random.bytes
* random.c (random_s_bytes): new method Random.bytes, which is equivalent to Random::DEFAULT.bytes. [Feature #4938] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62497 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/random.c b/random.c
index 43a3d2f..b800f1d 100644
--- a/random.c
+++ b/random.c
@@ -1134,6 +1134,19 @@ rb_random_bytes(VALUE obj, long n)
return genrand_bytes(rnd, n);
}
+/*
+ * call-seq: Random.bytes(size) -> a_string
+ *
+ * Returns a random binary string. The argument size specified the length of
+ * the result string.
+ */
+static VALUE
+random_s_bytes(VALUE obj, VALUE len)
+{
+ rb_random_t *rnd = rand_start(&default_rand);
+ return genrand_bytes(rnd, NUM2LONG(rb_to_int(len)));
+}
+
static VALUE
range_values(VALUE vmax, VALUE *begp, VALUE *endp, int *exclp)
{
@@ -1636,6 +1649,7 @@ InitVM_Random(void)
rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1);
rb_define_singleton_method(rb_cRandom, "rand", random_s_rand, -1);
+ rb_define_singleton_method(rb_cRandom, "bytes", random_s_bytes, 1);
rb_define_singleton_method(rb_cRandom, "new_seed", random_seed, 0);
rb_define_singleton_method(rb_cRandom, "urandom", random_raw_seed, 1);
rb_define_private_method(CLASS_OF(rb_cRandom), "state", random_s_state, 0);