Created
April 21, 2010 15:05
-
-
Save krohrbaugh/373923 to your computer and use it in GitHub Desktop.
Revisions
-
krohrbaugh revised this gist
Apr 21, 2010 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,6 +24,12 @@ } =end # Non-destructive shuffle def shuffle(array) shuffled = array.dup shuffle!(shuffled) end # Here's a basic translation of the C into Ruby def shuffle!(array) @@ -48,8 +54,8 @@ def shuffle!(array) y = ["a","b","c","d"] z = ["pretty","cool","right"] puts "#{v} => #{shuffle(v)}" puts "#{w} => #{shuffle(w)}" puts "#{x} => #{shuffle(x)}" puts "#{y} => #{shuffle(y)}" puts "#{z} => #{shuffle(z)}" -
krohrbaugh revised this gist
Apr 21, 2010 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,13 +26,10 @@ # Here's a basic translation of the C into Ruby def shuffle!(array) i = array.size while i > 0 do random_index = rand(i) i -= 1 -
krohrbaugh revised this gist
Apr 21, 2010 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,11 +45,13 @@ def shuffle!(array) end # Let's see if it works v = [1] w = [] x = [1,2,3,4,5,6] y = ["a","b","c","d"] z = ["pretty","cool","right"] puts "#{v} => #{shuffle!(v)}" puts "#{w} => #{shuffle!(w)}" puts "#{x} => #{shuffle!(x)}" puts "#{y} => #{shuffle!(y)}" -
krohrbaugh revised this gist
Apr 21, 2010 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,8 +26,12 @@ # Here's a basic translation of the C into Ruby def shuffle!(array) # have to bounds check for empty arrays return array if array.empty? i = array.size while i >= 0 do random_index = rand(i) i -= 1 @@ -41,10 +45,12 @@ def shuffle!(array) end # Let's see if it works w = [] x = [1,2,3,4,5,6] y = ["a","b","c","d"] z = ["pretty","cool","right"] puts "#{w} => #{shuffle!(w)}" puts "#{x} => #{shuffle!(x)}" puts "#{y} => #{shuffle!(y)}" puts "#{z} => #{shuffle!(z)}" -
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ =begin This is the actual C-based implementation of Array#shuffle! in ruby-1.9: /* * call-seq: * array.shuffle! -> array * * Shuffles elements in _self_ in place. */ static VALUE rb_ary_shuffle_bang(VALUE ary) { long i = RARRAY_LEN(ary); rb_ary_modify(ary); while (i) { long j = rb_genrand_real()*i; VALUE tmp = RARRAY_PTR(ary)[--i]; RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j]; RARRAY_PTR(ary)[j] = tmp; } return ary; } =end # Here's a basic translation of the C into Ruby def shuffle!(array) i = array.size while i >= 0 do random_index = rand(i) i -= 1 temp_val = array[i] array[i] = array[random_index] array[random_index] = temp_val end array end # Let's see if it works x = [1,2,3,4,5,6] y = ["a","b","c","d"] z = ["pretty","cool","right"] puts "#{x} => #{shuffle!(x)}" puts "#{y} => #{shuffle!(y)}" puts "#{z} => #{shuffle!(z)}"