Skip to content

Instantly share code, notes, and snippets.

@krohrbaugh
Created April 21, 2010 15:05
Show Gist options
  • Select an option

  • Save krohrbaugh/373923 to your computer and use it in GitHub Desktop.

Select an option

Save krohrbaugh/373923 to your computer and use it in GitHub Desktop.

Revisions

  1. krohrbaugh revised this gist Apr 21, 2010. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions shuffle_array.rb
    Original 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)}"
    puts "#{v} => #{shuffle(v)}"
    puts "#{w} => #{shuffle(w)}"
    puts "#{x} => #{shuffle(x)}"
    puts "#{y} => #{shuffle(y)}"
    puts "#{z} => #{shuffle(z)}"
  2. krohrbaugh revised this gist Apr 21, 2010. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions shuffle_array.rb
    Original 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)

    # have to bounds check for empty arrays
    return array if array.empty?


    i = array.size

    while i >= 0 do
    while i > 0 do
    random_index = rand(i)
    i -= 1

  3. krohrbaugh revised this gist Apr 21, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions shuffle_array.rb
    Original 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)}"
  4. krohrbaugh revised this gist Apr 21, 2010. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion shuffle_array.rb
    Original 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)}"
  5. @invalid-email-address Anonymous created this gist Apr 21, 2010.
    50 changes: 50 additions & 0 deletions shuffle_array.rb
    Original 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)}"