[前][次][番号順一覧][スレッド一覧]

ruby-changes:62856

From: Nobuyoshi <ko1@a...>
Date: Mon, 7 Sep 2020 20:08:30 +0900 (JST)
Subject: [ruby-changes:62856] d6ec0ef59b (master): Added `get_real` interface

https://git.ruby-lang.org/ruby.git/commit/?id=d6ec0ef59b

From d6ec0ef59b4c7c95beaad09f77cb5f86a0901b97 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 15 Apr 2020 00:52:08 +0900
Subject: Added `get_real` interface


diff --git a/ext/-test-/random/loop.c b/ext/-test-/random/loop.c
index 92af1a9..246a1f5 100644
--- a/ext/-test-/random/loop.c
+++ b/ext/-test-/random/loop.c
@@ -7,10 +7,13 @@ typedef struct { https://github.com/ruby/ruby/blob/trunk/ext/-test-/random/loop.c#L7
     uint32_t num, idx, *buf;
 } rand_loop_t;
 
+static double loop_get_real(rb_random_t *rnd, int excl);
+
 RB_RANDOM_INTERFACE_DECLARE(loop)
 static const rb_random_interface_t random_loop_if = {
     32,
     RB_RANDOM_INTERFACE_DEFINE(loop)
+    loop_get_real,
 };
 
 static size_t
@@ -91,6 +94,13 @@ loop_get_int32(rb_random_t *rnd) https://github.com/ruby/ruby/blob/trunk/ext/-test-/random/loop.c#L94
     return 0;
 }
 
+static double
+loop_get_real(rb_random_t *rnd, int excl)
+{
+    uint32_t a = loop_get_int32(rnd);
+    return ldexp(a, -16);
+}
+
 void
 Init_random_loop(VALUE mod, VALUE base)
 {
diff --git a/include/ruby/random.h b/include/ruby/random.h
index 21b6796..4dd12d5 100644
--- a/include/ruby/random.h
+++ b/include/ruby/random.h
@@ -28,12 +28,14 @@ typedef struct { https://github.com/ruby/ruby/blob/trunk/include/ruby/random.h#L28
 typedef void rb_random_init_func(rb_random_t *, const uint32_t *, size_t);
 typedef unsigned int rb_random_get_int32_func(rb_random_t *);
 typedef void rb_random_get_bytes_func(rb_random_t *, void *, size_t);
+typedef double rb_random_get_real_func(rb_random_t *, int);
 
 typedef struct {
     size_t default_seed_bits;
     rb_random_init_func *init;
     rb_random_get_int32_func *get_int32;
     rb_random_get_bytes_func *get_bytes;
+    rb_random_get_real_func *get_real;
 } rb_random_interface_t;
 
 #define rb_rand_if(obj) \
diff --git a/random.c b/random.c
index 3fef5ce..75f4dba 100644
--- a/random.c
+++ b/random.c
@@ -989,6 +989,7 @@ random_real(VALUE obj, rb_random_t *rnd, int excl) https://github.com/ruby/ruby/blob/trunk/random.c#L989
     }
     else {
         const rb_random_interface_t *rng = try_rand_if(obj, rnd);
+        if (rng->get_real) return rng->get_real(rnd, excl);
         a = random_int32(rng, rnd);
         b = random_int32(rng, rnd);
     }
diff --git a/test/-ext-/test_random.rb b/test/-ext-/test_random.rb
index da716c7..838e5d2 100644
--- a/test/-ext-/test_random.rb
+++ b/test/-ext-/test_random.rb
@@ -16,5 +16,12 @@ module TestRandomExt https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_random.rb#L16
       rnd = Bug::Random::Loop.new(1)
       assert_equal(1, rnd.rand(10))
     end
+
+    def test_real
+      assert_equal(0.25, Bug::Random::Loop.new(1<<14).rand)
+      assert_equal(0.50, Bug::Random::Loop.new(2<<14).rand)
+      assert_equal(0.75, Bug::Random::Loop.new(3<<14).rand)
+      assert_equal(1.00, Bug::Random::Loop.new(4<<14).rand)
+    end
   end
 end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]