summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--array.c32
-rw-r--r--version.h6
3 files changed, 22 insertions, 20 deletions
diff --git a/ChangeLog b/ChangeLog
index 3b5f43c294..2a8e4a9259 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Sat Oct 25 00:14:41 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * array.c (rb_ary_sample): fixed sizes and randomness.
+
Fri Oct 24 23:04:42 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
* configure.in (sitedir): considers --program-prefix and
diff --git a/array.c b/array.c
index 866849e6d2..e04de09cc2 100644
--- a/array.c
+++ b/array.c
@@ -3275,33 +3275,30 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
case 2:
i = rb_genrand_real()*len;
j = rb_genrand_real()*(len-1);
- if (j == i) j++;
+ if (j >= i) j++;
return rb_ary_new3(2, ptr[i], ptr[j]);
case 3:
i = rb_genrand_real()*len;
j = rb_genrand_real()*(len-1);
k = rb_genrand_real()*(len-2);
- if (j == i) j++;
- if ((k == i) ? (++k == j) : (k == j) ? (++k == i): 0) ++k;
+ {
+ long l = j, g = i;
+ if (j >= i) l = i, g = ++j;
+ if (k >= l && (++k >= g)) ++k;
+ }
return rb_ary_new3(3, ptr[i], ptr[j], ptr[k]);
}
if (n < sizeof(idx)/sizeof(idx[0])) {
- idx[0] = rb_genrand_real()*len;
+ long sorted[sizeof(idx)/sizeof(idx[0])];
+ sorted[0] = idx[0] = rb_genrand_real()*len;
for (i=1; i<n; i++) {
- long p = i;
k = rb_genrand_real()*--len;
- retry:
- j = 0;
- do {
- if (idx[j] == k) {
- ++k;
- if (p < j) goto retry;
- }
- else if (idx[j] > k) {
- if (p > j) p = j;
- }
- } while (++j < i);
- idx[i] = k;
+ for (j = 0; j < i; ++j) {
+ if (k < sorted[j]) break;
+ ++k;
+ }
+ memmove(&sorted[j+1], &sorted[j], sizeof(sorted[0])*(i-j));
+ sorted[j] = idx[i] = k;
}
result = rb_ary_new2(n);
for (i=0; i<n; i++) {
@@ -3318,6 +3315,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
RARRAY_PTR(result)[i] = nv;
}
}
+ ARY_SET_LEN(result, n);
return result;
}
diff --git a/version.h b/version.h
index 04678083d8..f3ca3c164f 100644
--- a/version.h
+++ b/version.h
@@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0"
-#define RUBY_RELEASE_DATE "2008-10-24"
+#define RUBY_RELEASE_DATE "2008-10-25"
#define RUBY_VERSION_CODE 190
-#define RUBY_RELEASE_CODE 20081024
+#define RUBY_RELEASE_CODE 20081025
#define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1
@@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2008
#define RUBY_RELEASE_MONTH 10
-#define RUBY_RELEASE_DAY 24
+#define RUBY_RELEASE_DAY 25
#ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[];