Skip to content

Commit

Permalink
* array.c (rb_ary_shuffle_bang): new method.
Browse files Browse the repository at this point in the history
* array.c (rb_ary_shuffle): ditto.

* random.c (genrand_real): ditto.

* random.c (genrand_int32): export the function.

* random.c (Init_Random): initialize random seed at the
  beginning.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10808 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Aug 31, 2006
1 parent b8597cc commit 22f249e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
13 changes: 13 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
Thu Aug 31 17:16:19 2006 Yukihiro Matsumoto <[email protected]>

* array.c (rb_ary_shuffle_bang): new method.

* array.c (rb_ary_shuffle): ditto.

* random.c (genrand_real): ditto.

* random.c (genrand_int32): export the function.

* random.c (Init_Random): initialize random seed at the
beginning.

Thu Aug 31 13:12:06 2006 why the lucky stiff <[email protected]>

* eval.c (ruby_init): rename top_cref to ruby_top_cref and export,
Expand Down
47 changes: 45 additions & 2 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,11 @@ rb_ary_join(VALUE ary, VALUE sep)

for (i=0; i<RARRAY(ary)->len; i++) {
tmp = rb_check_string_type(RARRAY(ary)->ptr[i]);
len += NIL_P(tmp) ? 10 : RSTRING(tmp)->len;
len += NIL_P(tmp) ? 10 : RSTRING_LEN(tmp);
}
if (!NIL_P(sep)) {
StringValue(sep);
len += RSTRING(sep)->len * (RARRAY(ary)->len - 1);
len += RSTRING_LEN(sep) * (RARRAY(ary)->len - 1);
}
result = rb_str_buf_new(len);
for (i=0; i<RARRAY(ary)->len; i++) {
Expand Down Expand Up @@ -2799,6 +2799,47 @@ rb_ary_flatten(int argc, VALUE *argv, VALUE ary)
return ary;
}

/*
* call-seq:
* array.shuffle! -> array or
*
* Shuffles elements in _self_ in place.
*/


static VALUE
rb_ary_shuffle_bang(VALUE ary)
{
long i = RARRAY(ary)->len;

while (i) {
long j = genrand_real()*i;
VALUE tmp = RARRAY(ary)->ptr[--i];
RARRAY(ary)->ptr[i] = RARRAY(ary)->ptr[j];
RARRAY(ary)->ptr[j] = tmp;
}
return ary;
}


/*
* call-seq:
* array.shuffle -> an_array
*
* Returns a new array that with elements of this array shuffled.
*
* s = [ 1, 2, 3 ] #=> [1, 2, 3]
* a.shuffle #=> [1, 2, 3]
*/

static VALUE
rb_ary_shuffle(VALUE ary)
{
ary = rb_ary_dup(ary);
rb_ary_shuffle_bang(ary);
return ary;
}


/* Arrays are ordered, integer-indexed collections of any object.
* Array indexing starts at 0, as in C or Java. A negative index is
Expand Down Expand Up @@ -2893,6 +2934,8 @@ Init_Array(void)
rb_define_method(rb_cArray, "flatten", rb_ary_flatten, -1);
rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, -1);
rb_define_method(rb_cArray, "nitems", rb_ary_nitems, 0);
rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0);
rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0);

id_cmp = rb_intern("<=>");
}
4 changes: 4 additions & 0 deletions intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ VALUE rb_detach_process(int);
VALUE rb_range_new(VALUE, VALUE, int);
VALUE rb_range_beg_len(VALUE, long*, long*, long, int);
VALUE rb_length_by_each(VALUE);
/* random.c */
unsigned long genrand_int32(void);
double genrand_real(void);
/* re.c */
int rb_memcmp(const void*,const void*,long);
int rb_memcicmp(const void*,const void*,long);
Expand Down Expand Up @@ -497,6 +500,7 @@ VALUE rb_str_times(VALUE, VALUE);
VALUE rb_str_substr(VALUE, long, long);
void rb_str_modify(VALUE);
VALUE rb_str_freeze(VALUE);
void rb_str_set_len(VALUE, long);
VALUE rb_str_resize(VALUE, long);
VALUE rb_str_cat(VALUE, const char*, long);
VALUE rb_str_cat2(VALUE, const char*);
Expand Down
10 changes: 3 additions & 7 deletions random.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ next_state(void)
}

/* generates a random number on [0,0xffffffff]-interval */
static unsigned long
unsigned long
genrand_int32(void)
{
unsigned long y;
Expand All @@ -163,7 +163,7 @@ genrand_int32(void)
}

/* generates a random number on [0,1) with 53-bit resolution*/
static double
double
genrand_real(void)
{
unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
Expand All @@ -188,7 +188,6 @@ genrand_real(void)
#include <fcntl.h>
#endif

static int first = 1;
static VALUE saved_seed = INT2FIX(0);

static VALUE
Expand Down Expand Up @@ -243,7 +242,6 @@ rand_init(VALUE vseed)
len--;
init_by_array(buf, len);
}
first = 0;
old = saved_seed;
saved_seed = seed;
free(buf);
Expand Down Expand Up @@ -437,9 +435,6 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
long val, max;

rb_scan_args(argc, argv, "01", &vmax);
if (first) {
rand_init(random_seed());
}
switch (TYPE(vmax)) {
case T_FLOAT:
if (RFLOAT(vmax)->value <= LONG_MAX && RFLOAT(vmax)->value >= LONG_MIN) {
Expand Down Expand Up @@ -489,6 +484,7 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
void
Init_Random(void)
{
rand_init(random_seed());
rb_define_global_function("srand", rb_f_srand, -1);
rb_define_global_function("rand", rb_f_rand, -1);
rb_global_variable(&saved_seed);
Expand Down

0 comments on commit 22f249e

Please sign in to comment.