<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1377,8 +1377,8 @@ rb_big_eql(VALUE x, SEL sel, VALUE y)
  * Unary minus (returns a new Bignum whose value is 0-big)
  */
 
-static VALUE
-rb_big_uminus(VALUE x, SEL sel)
+VALUE
+rb_big_uminus(VALUE x)
 {
     VALUE z = rb_big_clone(x);
 
@@ -1387,6 +1387,12 @@ rb_big_uminus(VALUE x, SEL sel)
     return bignorm(z);
 }
 
+static VALUE
+rb_big_uminus_imp(VALUE x, SEL sel)
+{
+    return rb_big_uminus(x);
+}
+
 /*
  * call-seq:
  *     ~big  =&gt;  integer
@@ -2833,7 +2839,7 @@ Init_Bignum(void)
 
     rb_objc_define_method(rb_cBignum, &quot;to_s&quot;, rb_big_to_s, -1);
     rb_objc_define_method(rb_cBignum, &quot;coerce&quot;, rb_big_coerce, 1);
-    rb_objc_define_method(rb_cBignum, &quot;-@&quot;, rb_big_uminus, 0);
+    rb_objc_define_method(rb_cBignum, &quot;-@&quot;, rb_big_uminus_imp, 0);
     rb_objc_define_method(rb_cBignum, &quot;+&quot;, rb_big_plus_imp, 1);
     rb_objc_define_method(rb_cBignum, &quot;-&quot;, rb_big_minus_imp, 1);
     rb_objc_define_method(rb_cBignum, &quot;*&quot;, rb_big_mul_imp, 1);</diff>
      <filename>bignum.c</filename>
    </modified>
    <modified>
      <diff>@@ -124,6 +124,7 @@ VALUE rb_big_or(VALUE, VALUE);
 VALUE rb_big_xor(VALUE, VALUE);
 VALUE rb_big_lshift(VALUE, VALUE);
 VALUE rb_big_rshift(VALUE, VALUE);
+VALUE rb_big_uminus(VALUE x);
 /* rational.c */
 VALUE rb_rational_raw(VALUE, VALUE);
 #define rb_rational_raw1(x) rb_rational_raw(x, INT2FIX(1))
@@ -415,6 +416,8 @@ VALUE rb_num_coerce_relop(VALUE, VALUE, SEL);
 VALUE rb_float_new(double);
 VALUE rb_num2fix(VALUE);
 VALUE rb_fix2str(VALUE, int);
+VALUE rb_fix_minus(VALUE x, VALUE y);
+VALUE rb_fix_uminus(VALUE num);
 VALUE rb_dbl_cmp(double, double);
 /* object.c */
 VALUE rb_send_dup(VALUE);</diff>
      <filename>include/ruby/intern.h</filename>
    </modified>
    <modified>
      <diff>@@ -2131,10 +2131,17 @@ rb_flo_induced_from(VALUE klass, SEL sel, VALUE x)
  * Negates &lt;code&gt;fix&lt;/code&gt; (which might return a Bignum).
  */
 
+VALUE
+rb_fix_uminus(VALUE num)
+{
+    return LONG2NUM(-FIX2LONG(num));
+}
+
+
 static VALUE
 fix_uminus(VALUE num, SEL sel)
 {
-    return LONG2NUM(-FIX2LONG(num));
+    return rb_fix_uminus(num);
 }
 
 VALUE</diff>
      <filename>numeric.c</filename>
    </modified>
    <modified>
      <diff>@@ -9,177 +9,13 @@
 
 **********************************************************************/
 
-/* 
-This is based on trimmed version of MT19937.  To get the original version,
-contact &lt;http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html&gt;.
-
-The original copyright notice follows.
-
-   A C-program for MT19937, with initialization improved 2002/2/10.
-   Coded by Takuji Nishimura and Makoto Matsumoto.
-   This is a faster version by taking Shawn Cokus's optimization,
-   Matthe Bellew's simplification, Isaku Wada's real version.
-
-   Before using, initialize the state by using init_genrand(seed) 
-   or init_by_array(init_key, key_length).
-
-   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
-   All rights reserved.                          
-
-   Redistribution and use in source and binary forms, with or without
-   modification, are permitted provided that the following conditions
-   are met:
-
-     1. Redistributions of source code must retain the above copyright
-        notice, this list of conditions and the following disclaimer.
-
-     2. Redistributions in binary form must reproduce the above copyright
-        notice, this list of conditions and the following disclaimer in the
-        documentation and/or other materials provided with the distribution.
-
-     3. The names of its contributors may not be used to endorse or promote 
-        products derived from this software without specific prior written 
-        permission.
-
-   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-   &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
-   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-
-   Any feedback is very welcome.
-   http://www.math.keio.ac.jp/matumoto/emt.html
-   email: matumoto@math.keio.ac.jp
-*/
-
-/* Period parameters */  
-#define N 624
-#define M 397
-#define MATRIX_A 0x9908b0dfUL   /* constant vector a */
-#define UMASK 0x80000000UL /* most significant w-r bits */
-#define LMASK 0x7fffffffUL /* least significant r bits */
-#define MIXBITS(u,v) ( ((u) &amp; UMASK) | ((v) &amp; LMASK) )
-#define TWIST(u,v) ((MIXBITS(u,v) &gt;&gt; 1) ^ ((v)&amp;1UL ? MATRIX_A : 0UL))
-
-static unsigned long state[N]; /* the array for the state vector  */
-static int left = 1;
-static int initf = 0;
-static unsigned long *next;
-
-/* initializes state[N] with a seed */
-static void
-init_genrand(unsigned long s)
-{
-    int j;
-    state[0]= s &amp; 0xffffffffUL;
-    for (j=1; j&lt;N; j++) {
-        state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] &gt;&gt; 30)) + j); 
-        /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
-        /* In the previous versions, MSBs of the seed affect   */
-        /* only MSBs of the array state[].                        */
-        /* 2002/01/09 modified by Makoto Matsumoto             */
-        state[j] &amp;= 0xffffffffUL;  /* for &gt;32 bit machines */
-    }
-    left = 1; initf = 1;
-}
-
-/* initialize by an array with array-length */
-/* init_key is the array for initializing keys */
-/* key_length is its length */
-/* slight change for C++, 2004/2/26 */
-static void
-init_by_array(unsigned long init_key[], int key_length)
-{
-    int i, j, k;
-    init_genrand(19650218UL);
-    i=1; j=0;
-    k = (N&gt;key_length ? N : key_length);
-    for (; k; k--) {
-        state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] &gt;&gt; 30)) * 1664525UL))
-          + init_key[j] + j; /* non linear */
-        state[i] &amp;= 0xffffffffUL; /* for WORDSIZE &gt; 32 machines */
-        i++; j++;
-        if (i&gt;=N) { state[0] = state[N-1]; i=1; }
-        if (j&gt;=key_length) j=0;
-    }
-    for (k=N-1; k; k--) {
-        state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] &gt;&gt; 30)) * 1566083941UL))
-          - i; /* non linear */
-        state[i] &amp;= 0xffffffffUL; /* for WORDSIZE &gt; 32 machines */
-        i++;
-        if (i&gt;=N) { state[0] = state[N-1]; i=1; }
-    }
-
-    state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
-    left = 1; initf = 1;
-}
-
-static void
-next_state(void)
-{
-    unsigned long *p=state;
-    int j;
-
-    /* if init_genrand() has not been called, */
-    /* a default initial seed is used         */
-    if (initf==0) init_genrand(5489UL);
-
-    left = N;
-    next = state;
-    
-    for (j=N-M+1; --j; p++) 
-        *p = p[M] ^ TWIST(p[0], p[1]);
-
-    for (j=M; --j; p++) 
-        *p = p[M-N] ^ TWIST(p[0], p[1]);
-
-    *p = p[M-N] ^ TWIST(p[0], state[0]);
-}
-
-/* generates a random number on [0,0xffffffff]-interval */
-static unsigned long
-genrand_int32(void)
-{
-    unsigned long y;
-
-    if (--left == 0) next_state();
-    y = *next++;
-
-    /* Tempering */
-    y ^= (y &gt;&gt; 11);
-    y ^= (y &lt;&lt; 7) &amp; 0x9d2c5680UL;
-    y ^= (y &lt;&lt; 15) &amp; 0xefc60000UL;
-    y ^= (y &gt;&gt; 18);
-
-    return y;
-}
-
-/* generates a random number on [0,1) with 53-bit resolution*/
-static double
-genrand_real(void)
-{ 
-    unsigned long a=genrand_int32()&gt;&gt;5, b=genrand_int32()&gt;&gt;6; 
-    return(a*67108864.0+b)*(1.0/9007199254740992.0); 
-} 
-/* These real versions are due to Isaku Wada, 2002/01/09 added */
-
-#undef N
-#undef M
-
-/* These real versions are due to Isaku Wada, 2002/01/09 added */
-
 #include &quot;ruby/ruby.h&quot;
 
 #ifdef HAVE_UNISTD_H
 #include &lt;unistd.h&gt;
 #endif
+#include &lt;limits.h&gt;
+#include &lt;math.h&gt;
 #include &lt;time.h&gt;
 #include &lt;sys/types.h&gt;
 #include &lt;sys/stat.h&gt;
@@ -187,129 +23,107 @@ genrand_real(void)
 #include &lt;fcntl.h&gt;
 #endif
 
-unsigned long
-rb_genrand_int32(void)
+#include &quot;ruby/node.h&quot;
+#include &quot;vm.h&quot;
+
+
+static void
+init_random(void)
 {
-    return genrand_int32();
+    srandomdev();
 }
 
-double
-rb_genrand_real(void)
+/*
+ * #rand(0) needs to generate a float x, such as 0.0 &lt;= x &lt;= 1.0. However,
+ * srandom() returns a long. We want to convert that long by dividing it until
+ * it is &lt;= 1.0 using its 10th power.
+ * For performance reasons, long_to_float_divider() returns the 10th power of
+ * the number, without calculating the number length and using pow().
+ */
+static inline unsigned long
+long_to_float_divider(long val)
 {
-    return genrand_real();
-}
+    unsigned long divider = 1;
 
-static VALUE saved_seed = INT2FIX(0);
+    while (divider &lt;= val) {
+        divider *= 10;
+    }
+    return divider;
+}
 
 static VALUE
-rand_init(VALUE vseed)
+random_number_with_limit(long limit)
 {
-    volatile VALUE seed;
-    VALUE old;
-    long len;
-    unsigned long *buf;
+    long	val;
 
-    seed = rb_to_int(vseed);
-    switch (TYPE(seed)) {
-      case T_FIXNUM:
-          len = sizeof(VALUE);
-          break;
-      case T_BIGNUM:
-          len = RBIGNUM_LEN(seed) * SIZEOF_BDIGITS;
-          if (len == 0)
-              len = 4;
-          break;
-      default:
-          rb_raise(rb_eTypeError, &quot;failed to convert %s into Integer&quot;,
-                   rb_obj_classname(vseed));
-    }
-    len = (len + 3) / 4; /* number of 32bit words */
-    buf = ALLOC_N(unsigned long, len); /* allocate longs for init_by_array */
-    memset(buf, 0, len * sizeof(long));
-    if (FIXNUM_P(seed)) {
-        buf[0] = FIX2ULONG(seed) &amp; 0xffffffff;
-#if SIZEOF_LONG &gt; 4
-        buf[1] = FIX2ULONG(seed) &gt;&gt; 32;
-#endif
-    }
-    else {
-        int i, j;
-        for (i = RBIGNUM_LEN(seed)-1; 0 &lt;= i; i--) {
-            j = i * SIZEOF_BDIGITS / 4;
-#if SIZEOF_BDIGITS &lt; 4
-            buf[j] &lt;&lt;= SIZEOF_BDIGITS * 8;
-#endif
-            buf[j] |= RBIGNUM_DIGITS(seed)[i];
-        }
-    }
-    while (1 &lt; len &amp;&amp; buf[len-1] == 0) {
-        len--;
-    }
-    if (len &lt;= 1) {
-        init_genrand(buf[0]);
+    if (limit == 0) {
+#if __LP64__
+	val = random();
+#else
+	val = random() % (ULONG_MAX / 10);
+#endif /* !__LP64__ */
+	return DOUBLE2NUM((double)val / long_to_float_divider(val));
     }
     else {
-        if (buf[len-1] == 1) /* remove leading-zero-guard */
-            len--;
-        init_by_array(buf, len);
+	if (limit &lt; 0) {
+	    /* -LONG_MIN = LONG_MIN, let's avoid that */
+	    if (limit == LONG_MIN) {
+		limit += 1;
+	    }
+	    limit = -limit;
+	}
+	val = random() % limit;
+	return LONG2NUM(val);
     }
-    old = saved_seed;
-    saved_seed = seed;
-    xfree(buf);
-    return old;
+    return Qnil;
 }
 
+unsigned long rb_genrand_int32(void);
+
 static VALUE
-random_seed(void)
+random_number_with_bignum_limit(struct RBignum *bignum_limit)
 {
-    static int n = 0;
-    struct timeval tv;
-    int fd;
-    struct stat statbuf;
-
-    int seed_len;
-    BDIGIT *digits;
-    unsigned long *seed;
-    NEWOBJ(big, struct RBignum);
-    OBJSETUP(big, rb_cBignum, T_BIGNUM);
-
-    seed_len = 4 * sizeof(long);
-    RBIGNUM_SET_SIGN(big, 1);
-    rb_big_resize((VALUE)big, seed_len / SIZEOF_BDIGITS + 1);
-    digits = RBIGNUM_DIGITS(big);
-    seed = (unsigned long *)RBIGNUM_DIGITS(big);
-
-    memset(digits, 0, RBIGNUM_LEN(big) * SIZEOF_BDIGITS);
-
-#ifdef S_ISCHR
-    if ((fd = open(&quot;/dev/urandom&quot;, O_RDONLY
-#ifdef O_NONBLOCK
-            |O_NONBLOCK
-#endif
-#ifdef O_NOCTTY
-            |O_NOCTTY
-#endif
-#ifdef O_NOFOLLOW
-            |O_NOFOLLOW
-#endif
-            )) &gt;= 0) {
-        if (fstat(fd, &amp;statbuf) == 0 &amp;&amp; S_ISCHR(statbuf.st_mode)) {
-            read(fd, seed, seed_len);
-        }
-        close(fd);
+    long	   nb_long_in_bignum;
+    long           nb_loops;
+    struct RBignum *div_result;
+    struct RBignum *result;
+    int		   idx;
+
+    div_result = RBIGNUM(rb_big_div((VALUE)bignum_limit, LONG2NUM(LONG_MAX)));
+    // FIXME: Shouldn't use !, what value should I check?
+    if (!FIXNUM_P((VALUE)div_result)) {
+	rb_raise(rb_eArgError, &quot;max is too huge&quot;);
     }
-#endif
 
-    gettimeofday(&amp;tv, 0);
-    seed[0] ^= tv.tv_usec;
-    seed[1] ^= tv.tv_sec;
-    seed[2] ^= getpid() ^ (n++ &lt;&lt; 16);
-    seed[3] ^= (unsigned long)&amp;seed;
+    nb_long_in_bignum = FIX2LONG((VALUE)div_result);
+    nb_loops = 1 + FIX2LONG(random_number_with_limit(nb_long_in_bignum));
+    result = RBIGNUM(rb_int2big(0));
+    for (idx = 0; idx &lt; nb_loops; idx++) {
+	// This creates a bignum on each iteration... Not really good :-/
+	result = RBIGNUM(rb_big_plus((VALUE)result, random_number_with_limit(LONG_MAX)));
+    }
+    return ((VALUE)result);
+}
 
-    /* set leading-zero-guard if need. */
-    digits[RBIGNUM_LEN(big)-1] = digits[RBIGNUM_LEN(big)-2] &lt;= 1 ? 1 : 0;
+unsigned long
+rb_genrand_int32(void)
+{
+    unsigned long result;
+    short	  nb_loops;
+    int		  idx;
+
+    result = 0;
+    nb_loops = 1 + (random() % 2);
+    for (idx = 0; idx &lt; nb_loops; idx++) {
+	result += random();
+    }
+    return result;
+}
 
-    return rb_big_norm((VALUE)big);
+double
+rb_genrand_real(void)
+{
+    return NUM2DBL(random_number_with_limit(0));
 }
 
 /*
@@ -329,101 +143,49 @@ random_seed(void)
 static VALUE
 rb_f_srand(VALUE obj, SEL sel, int argc, VALUE *argv)
 {
-    VALUE seed, old;
+    VALUE	old_seed;
+    VALUE       seed_param;
+    VALUE	seed_int;
+    unsigned	seed = 0;
 
-    rb_secure(4);
     if (argc == 0) {
-	seed = random_seed();
+	srandomdev();
+	seed = (unsigned)random();
+	seed_int = UINT2NUM(seed);
     }
     else {
-	rb_scan_args(argc, argv, &quot;01&quot;, &amp;seed);
-    }
-    old = rand_init(seed);
-
-    return old;
-}
-
-static unsigned long 
-make_mask(unsigned long x)
-{
-    x = x | x &gt;&gt; 1;
-    x = x | x &gt;&gt; 2;
-    x = x | x &gt;&gt; 4;
-    x = x | x &gt;&gt; 8;
-    x = x | x &gt;&gt; 16;
-#if 4 &lt; SIZEOF_LONG
-    x = x | x &gt;&gt; 32;
-#endif
-    return x;
-}
-
-static unsigned long
-limited_rand(unsigned long limit)
-{
-    unsigned long mask = make_mask(limit);
-    int i;
-    unsigned long val;
-
-  retry:
-    val = 0;
-    for (i = SIZEOF_LONG/4-1; 0 &lt;= i; i--) {
-        if (mask &gt;&gt; (i * 32)) {
-            val |= genrand_int32() &lt;&lt; (i * 32);
-            val &amp;= mask;
-            if (limit &lt; val)
-                goto retry;
-        }
+	rb_scan_args(argc, argv, &quot;01&quot;, &amp;seed_param);
+	seed_int = rb_to_int(seed_param);
+	switch (TYPE(seed_int)) {
+	  case T_BIGNUM:
+	    // In case we need to keep 'seed.to_i.abs'
+	    /*
+	    if (RBIGNUM_NEGATIVE_P(seed_int)) {
+		seed_int = rb_big_uminus(seed_int);
+	    }
+	    */
+	    for (int i = 0; i &lt; RBIGNUM_LEN(seed_int); i++) {
+		seed += (unsigned int)RBIGNUM_DIGITS(seed_int)[i];
+	    }
+	    break ;
+	  case T_FIXNUM:
+	    // In case we need to keep 'seed.to_i.abs'
+	    /*
+	    if (FIX2LONG(seed_int) &lt; 0) {
+		seed_int = rb_fix_uminus(seed_int);
+	    }
+	    */
+	    seed = (unsigned int)FIX2LONG(seed_int);
+	    break ;
+	}
     }
-    return val;
-}
+    srandom(seed);
 
-static VALUE
-limited_big_rand(struct RBignum *limit)
-{
-    unsigned long mask, lim, rnd;
-    struct RBignum *val;
-    int i, len, boundary;
-
-    len = (RBIGNUM_LEN(limit) * SIZEOF_BDIGITS + 3) / 4;
-    val = (struct RBignum *)rb_big_clone((VALUE)limit);
-    RBIGNUM_SET_SIGN(val, 1);
-#if SIZEOF_BDIGITS == 2
-# define BIG_GET32(big,i) \
-    (RBIGNUM_DIGITS(big)[(i)*2] | \
-     ((i)*2+1 &lt; RBIGNUM_LEN(big) ? \
-      (RBIGNUM_DIGITS(big)[(i)*2+1] &lt;&lt; 16) : \
-      0))
-# define BIG_SET32(big,i,d) \
-    ((RBIGNUM_DIGITS(big)[(i)*2] = (d) &amp; 0xffff), \
-     ((i)*2+1 &lt; RBIGNUM_LEN(big) ? \
-      (RBIGNUM_DIGITS(big)[(i)*2+1] = (d) &gt;&gt; 16) : \
-      0))
-#else
-    /* SIZEOF_BDIGITS == 4 */
-# define BIG_GET32(big,i) (RBIGNUM_DIGITS(big)[i])
-# define BIG_SET32(big,i,d) (RBIGNUM_DIGITS(big)[i] = (d))
-#endif
-  retry:
-    mask = 0;
-    boundary = 1;
-    for (i = len-1; 0 &lt;= i; i--) {
-        lim = BIG_GET32(limit, i);
-        mask = mask ? 0xffffffff : make_mask(lim);
-        if (mask) {
-            rnd = genrand_int32() &amp; mask;
-            if (boundary) {
-                if (lim &lt; rnd)
-                    goto retry;
-                if (rnd &lt; lim)
-                    boundary = 0;
-            }
-        }
-        else {
-            rnd = 0;
-        }
-        BIG_SET32(val, i, rnd);
-    }
-    return rb_big_norm((VALUE)val);
+    old_seed = rb_vm_rand_seed();
+    // Ruby's behaviour is weird. It stores the 'seed.to_i' value, instead of
+    // the 'seed.to_i.abs' value, or just 'seed'. Which one should we use?
+    rb_vm_set_rand_seed(seed_int);
+    return old_seed;
 }
 
 /*
@@ -449,61 +211,54 @@ limited_big_rand(struct RBignum *limit)
 static VALUE
 rb_f_rand(VALUE obj, SEL sel, int argc, VALUE *argv)
 {
-    VALUE vmax;
-    long val, max;
+    VALUE	arg_max;
+    long	max;
 
-    rb_scan_args(argc, argv, &quot;01&quot;, &amp;vmax);
-    switch (TYPE(vmax)) {
+    rb_scan_args(argc, argv, &quot;01&quot;, &amp;arg_max);
+    switch (TYPE(arg_max)) {
       case T_FLOAT:
-	if (RFLOAT_VALUE(vmax) &lt;= LONG_MAX &amp;&amp; RFLOAT_VALUE(vmax) &gt;= LONG_MIN) {
-	    max = (long)RFLOAT_VALUE(vmax);
+	if (RFLOAT_VALUE(arg_max) &lt;= LONG_MAX &amp;&amp; RFLOAT_VALUE(arg_max) &gt;= LONG_MIN) {
+	    max = (long)RFLOAT_VALUE(arg_max);
 	    break;
 	}
-        if (RFLOAT_VALUE(vmax) &lt; 0)
-            vmax = rb_dbl2big(-RFLOAT_VALUE(vmax));
+        if (RFLOAT_VALUE(arg_max) &lt; 0)
+            arg_max = rb_dbl2big(-RFLOAT_VALUE(arg_max));
         else
-            vmax = rb_dbl2big(RFLOAT_VALUE(vmax));
+            arg_max = rb_dbl2big(RFLOAT_VALUE(arg_max));
 	/* fall through */
       case T_BIGNUM:
       bignum:
         {
-            struct RBignum *limit = (struct RBignum *)vmax;
+            struct RBignum *limit = (struct RBignum *)arg_max;
             if (!RBIGNUM_SIGN(limit)) {
-                limit = (struct RBignum *)rb_big_clone(vmax);
+                limit = (struct RBignum *)rb_big_clone(arg_max);
                 RBIGNUM_SET_SIGN(limit, 1);
             }
             limit = (struct RBignum *)rb_big_minus((VALUE)limit, INT2FIX(1));
             if (FIXNUM_P((VALUE)limit)) {
-                if (FIX2LONG((VALUE)limit) == -1)
-                    return DOUBLE2NUM(genrand_real());
-                return LONG2NUM(limited_rand(FIX2LONG((VALUE)limit)));
+		max = FIX2LONG((VALUE)limit) + 1;
+		break;
             }
-            return limited_big_rand(limit);
+            return random_number_with_bignum_limit(limit);
 	}
       case T_NIL:
 	max = 0;
 	break;
       default:
-	vmax = rb_Integer(vmax);
-	if (TYPE(vmax) == T_BIGNUM) goto bignum;
+	arg_max = rb_Integer(arg_max);
+	if (TYPE(arg_max) == T_BIGNUM) goto bignum;
       case T_FIXNUM:
-	max = FIX2LONG(vmax);
+	max = FIX2LONG(arg_max);
 	break;
     }
 
-    if (max == 0) {
-	return DOUBLE2NUM(genrand_real());
-    }
-    if (max &lt; 0) max = -max;
-    val = limited_rand(max-1);
-    return LONG2NUM(val);
+    return random_number_with_limit(max);
 }
 
 void
 Init_Random(void)
 {
-    rand_init(random_seed());
+    init_random();
     rb_objc_define_module_function(rb_mKernel, &quot;srand&quot;, rb_f_srand, -1);
     rb_objc_define_module_function(rb_mKernel, &quot;rand&quot;, rb_f_rand, -1);
-    rb_global_variable(&amp;saved_seed);
 }</diff>
      <filename>random.c</filename>
    </modified>
    <modified>
      <diff>@@ -190,6 +190,9 @@ RoxorCore::RoxorCore(void)
 
     pthread_assert(pthread_mutex_init(&amp;gl, 0));
 
+    // Fixnum is an immediate type, no need to retain
+    rand_seed = INT2FIX(0);
+
     load_path = rb_ary_new();
     rb_objc_retain((void *)load_path);
 
@@ -920,6 +923,35 @@ rb_vm_set_running(bool flag)
 }
 
 extern &quot;C&quot;
+VALUE
+rb_vm_rand_seed(void)
+{
+    VALUE     rand_seed;
+    RoxorCore *core = GET_CORE();
+
+    core-&gt;lock();
+    rand_seed = core-&gt;get_rand_seed();
+    core-&gt;unlock();
+
+    return rand_seed;
+}
+
+extern &quot;C&quot;
+void
+rb_vm_set_rand_seed(VALUE rand_seed)
+{
+    RoxorCore *core = GET_CORE();
+
+    core-&gt;lock();
+    if (core-&gt;get_rand_seed() != rand_seed) {
+	GC_RELEASE(core-&gt;get_rand_seed());
+	GC_RETAIN(rand_seed);
+	core-&gt;set_rand_seed(rand_seed);
+    }
+    core-&gt;unlock();
+}
+
+extern &quot;C&quot;
 bool
 rb_vm_abort_on_exception(void)
 {</diff>
      <filename>vm.cpp</filename>
    </modified>
    <modified>
      <diff>@@ -259,6 +259,8 @@ void rb_vm_aot_compile(NODE *node);
 void rb_vm_init_compiler(void);
 bool rb_vm_running(void);
 void rb_vm_set_running(bool flag);
+VALUE rb_vm_rand_seed(void);
+void rb_vm_set_rand_seed(VALUE rand_seed);
 bool rb_vm_parse_in_eval(void);
 void rb_vm_set_parse_in_eval(bool flag);
 VALUE rb_vm_load_path(void);
@@ -551,6 +553,7 @@ class RoxorCore {
 	pthread_mutex_t gl;
 	VALUE loaded_features;
 	VALUE load_path;
+	VALUE rand_seed;
 
 	// Cache to avoid compiling the same Function twice.
 	std::map&lt;Function *, IMP&gt; JITcache;
@@ -603,6 +606,7 @@ class RoxorCore {
 	ACCESSOR(running, bool);
 	ACCESSOR(multithreaded, bool);
 	ACCESSOR(abort_on_exception, bool);
+	ACCESSOR(rand_seed, VALUE);
 	READER(loaded_features, VALUE);
 	READER(load_path, VALUE);
 	READER(threads, VALUE);</diff>
      <filename>vm.h</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>4290e805c4771de36c0b66637d20f2272fff7dbf</id>
    </parent>
  </parents>
  <author>
    <name>lsansonetti@apple.com</name>
    <email>lsansonetti@apple.com@23306eb0-4c56-4727-a40e-e92c0eb68959</email>
  </author>
  <url>http://github.com/masterkain/macruby/commit/4a603c87e2afa700a455a9e8cadafe65575d3f94</url>
  <id>4a603c87e2afa700a455a9e8cadafe65575d3f94</id>
  <committed-date>2009-10-15T18:37:01-07:00</committed-date>
  <authored-date>2009-10-15T18:37:01-07:00</authored-date>
  <message>re-implemented #rand and #srand to use random(3) and srandom(3), faster and thread-safe (patch contributed by Thibault Martin-Lagardette)

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@2826 23306eb0-4c56-4727-a40e-e92c0eb68959</message>
  <tree>63e6b0c8a9baba9f348db6c3db8e53519463a021</tree>
  <committer>
    <name>lsansonetti@apple.com</name>
    <email>lsansonetti@apple.com@23306eb0-4c56-4727-a40e-e92c0eb68959</email>
  </committer>
</commit>
