Project

General

Profile

Feature #4938

Add Random.bytes [patch]

Added by marcandre (Marc-Andre Lafortune) over 6 years ago. Updated 5 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:37576]

Description

It could be useful to add a Random.bytes method (equivalent to Random::DEFAULT.bytes), as per the simple patch attached.

randombytes.diff (2.19 KB) randombytes.diff marcandre (Marc-Andre Lafortune), 06/28/2011 12:58 AM
randombytes-ruby22.diff (2.38 KB) randombytes-ruby22.diff hsbt (Hiroshi SHIBATA), 10/29/2014 05:37 AM

Associated revisions

Revision 62497
Added by nobu (Nobuyoshi Nakada) 5 days ago

random.c: Random.bytes

  • random.c (random_s_bytes): new method Random.bytes, which is equivalent to Random::DEFAULT.bytes. [Feature #4938]

History

#1 Updated by shyouhei (Shyouhei Urabe) almost 6 years ago

  • Status changed from Open to Assigned

#2 [ruby-core:49243] Updated by marcandre (Marc-Andre Lafortune) over 5 years ago

Comment about this trivial feature would be appreciated.

#3 [ruby-core:49248] Updated by mrkn (Kenta Murata) over 5 years ago

  • Target version changed from 2.0.0 to 2.6

I think it is useful and acceptable to introduce, but 2.0 is already frozen about its features, so the target version should be next-minor.

#4 [ruby-core:65670] Updated by hsbt (Hiroshi SHIBATA) over 3 years ago

  • File randombytes-ruby22.diff added

I update this patch on Ruby 2.2.

#5 [ruby-core:65974] Updated by marcandre (Marc-Andre Lafortune) over 3 years ago

Hi Nobu,
is there a reason not to commit this?

#6 [ruby-core:65976] Updated by nobu (Nobuyoshi Nakada) over 3 years ago

  • Assignee changed from nobu (Nobuyoshi Nakada) to normalperson (Eric Wong)

I think there is no problems.

#7 [ruby-core:65978] Updated by hsbt (Hiroshi SHIBATA) over 3 years ago

  • File deleted (randombytes-ruby22.diff)

#9 [ruby-core:65982] Updated by normalperson (Eric Wong) over 3 years ago

shibata.hiroshi@gmail.com wrote:

File randombytes-ruby22.diff added

Looks good to me.
Any approval from matz since this is a new API?

#10 [ruby-core:66057] Updated by normalperson (Eric Wong) over 3 years ago

  • Assignee changed from normalperson (Eric Wong) to matz (Yukihiro Matsumoto)

Any comment from matz for API addition?

#11 Updated by naruse (Yui NARUSE) 2 months ago

  • Target version deleted (2.6)

#12 [ruby-core:85126] Updated by nobu (Nobuyoshi Nakada) about 1 month ago

Updated.

diff --git i/random.c w/random.c
index 43a3d2ffee..b800f1d9a1 100644
--- i/random.c
+++ w/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);
diff --git i/test/ruby/test_rand.rb w/test/ruby/test_rand.rb
index 882d33fb40..136ee4f912 100644
--- i/test/ruby/test_rand.rb
+++ w/test/ruby/test_rand.rb
@@ -347,10 +347,15 @@
   end

   def assert_random_bytes(r)
+    srand(0)
     assert_equal("", r.bytes(0))
-    assert_equal("\xAC".force_encoding("ASCII-8BIT"), r.bytes(1))
-    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"),
-                 r.bytes(10))
+    assert_equal("", Random.bytes(0))
+    x = "\xAC".force_encoding("ASCII-8BIT")
+    assert_equal(x, r.bytes(1))
+    assert_equal(x, Random.bytes(1))
+    x = "/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT")
+    assert_equal(x, r.bytes(10))
+    assert_equal(x, Random.bytes(10))
   end

   def test_random_range

#13 [ruby-core:85685] Updated by matz (Yukihiro Matsumoto) 5 days ago

Accepted. Better later than never.

Matz.

#14 Updated by nobu (Nobuyoshi Nakada) 5 days ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r62497.


random.c: Random.bytes

  • random.c (random_s_bytes): new method Random.bytes, which is equivalent to Random::DEFAULT.bytes. [Feature #4938]

Also available in: Atom PDF