[macruby-changes] [3013] MacRuby/trunk/random.c
source_changes at macosforge.org
source_changes at macosforge.org
Mon Nov 16 11:02:12 PST 2009
Revision: 3013
http://trac.macosforge.org/projects/ruby/changeset/3013
Author: lsansonetti at apple.com
Date: 2009-11-16 11:02:11 -0800 (Mon, 16 Nov 2009)
Log Message:
-----------
Fixed Kernel#rand(nil) random float generation (patch by nagachika00 at gmail.com)
Modified Paths:
--------------
MacRuby/trunk/random.c
Modified: MacRuby/trunk/random.c
===================================================================
--- MacRuby/trunk/random.c 2009-11-16 16:54:04 UTC (rev 3012)
+++ MacRuby/trunk/random.c 2009-11-16 19:02:11 UTC (rev 3013)
@@ -33,36 +33,18 @@
srandomdev();
}
-/*
- * #rand(0) needs to generate a float x, such as 0.0 <= x <= 1.0. However,
- * srandom() returns a long. We want to convert that long by dividing it until
- * it is <= 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)
-{
- unsigned long divider = 1;
-
- while (divider <= val) {
- divider *= 10;
- }
- return divider;
-}
-
static VALUE
random_number_with_limit(long limit)
{
long val;
if (limit == 0) {
-#if __LP64__
+ /*
+ * #rand(0) needs to generate a float x, such as 0.0 <= x <= 1.0. However,
+ * srandom() returns a long. We want to convert that long by divide by RAND_MAX
+ */
val = random();
-#else
- val = random() % (ULONG_MAX / 10);
-#endif /* !__LP64__ */
- return DOUBLE2NUM((double)val / long_to_float_divider(val));
+ return DOUBLE2NUM((double)val / ((unsigned long)RAND_MAX+1));
}
else {
if (limit < 0) {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20091116/f348f6ab/attachment.html>
More information about the macruby-changes
mailing list