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

ruby-changes:25073

From: nobu <ko1@a...>
Date: Tue, 9 Oct 2012 17:59:18 +0900 (JST)
Subject: [ruby-changes:25073] nobu:r37125 (trunk): random.c: rb_random_ulong_limited

nobu	2012-10-09 17:59:03 +0900 (Tue, 09 Oct 2012)

  New Revision: 37125

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37125

  Log:
    random.c: rb_random_ulong_limited
    
    * random.c (rb_random_ulong_limited): new function to return a random
      value from 0 upto limit as unsigned long, simillary to
      rb_genrand_ulong_limited but with arbitrary RNG object.

  Modified files:
    trunk/ChangeLog
    trunk/include/ruby/intern.h
    trunk/random.c

Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 37124)
+++ include/ruby/intern.h	(revision 37125)
@@ -610,6 +610,7 @@
 VALUE rb_random_int(VALUE rnd, VALUE max);
 unsigned int rb_random_int32(VALUE rnd);
 double rb_random_real(VALUE rnd);
+unsigned long rb_random_ulong_limited(VALUE rnd, unsigned long limit);
 unsigned long rb_genrand_ulong_limited(unsigned long i);
 /* re.c */
 #define rb_memcmp memcmp
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37124)
+++ ChangeLog	(revision 37125)
@@ -1,3 +1,9 @@
+Tue Oct  9 17:59:00 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* random.c (rb_random_ulong_limited): new function to return a random
+	  value from 0 upto limit as unsigned long, simillary to
+	  rb_genrand_ulong_limited but with arbitrary RNG object.
+
 Tue Oct  9 17:13:27 2012  Nobuyoshi Nakada  <nobu@r...>
 
 	* process.c (rb_execarg_addopt, rb_execarg_run_options): add :uid and
Index: random.c
===================================================================
--- random.c	(revision 37124)
+++ random.c	(revision 37125)
@@ -943,6 +943,22 @@
     return genrand_real(&rnd->mt);
 }
 
+unsigned long
+rb_random_ulong_limited(VALUE obj, unsigned long limit)
+{
+    rb_random_t *rnd = try_get_rnd(obj);
+    if (!rnd) {
+	VALUE lim = ULONG2NUM(limit);
+	VALUE v = rb_funcall2(obj, id_rand, 1, &lim);
+	unsigned long r = NUM2ULONG(v);
+	if (r > limit) {
+	    rb_raise(rb_eRangeError, "random number too big %ld", r);
+	}
+	return r;
+    }
+    return limited_rand(&rnd->mt, limit);
+}
+
 /*
  * call-seq: prng.bytes(size) -> a_string
  *

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

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