Navigation Menu

Skip to content

Commit

Permalink
Fixed shuffle algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfe committed Feb 7, 2015
1 parent 395d4c1 commit 83d671f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions go/shuffle/shuffle_test.go
Expand Up @@ -9,7 +9,6 @@ package shuffle
// $ go test -benchmem -bench=. shuffle_test.go -benchtime=10s

import (
"fmt"
"math/rand"
"reflect"
"testing"
Expand All @@ -30,17 +29,19 @@ func init() {

// Native shuffle. No generics. Shuffle a slice of ints.
func shuffle0(a []int) {
l := len(a)
for i := range a {
j := rand.Intn(i + 1)
j := i + rand.Intn(l-i)
a[i], a[j] = a[j], a[i]
}
}

// Technique 1: requires user to convert specificially typed slice to
// a slice of interface{}. Shuffles the copy in-place.
func shuffle1(a []interface{}) {
l := len(a)
for i := range a {
j := rand.Intn(i + 1)
j := i + rand.Intn(l-i)
a[i], a[j] = a[j], a[i]
}
}
Expand All @@ -62,9 +63,10 @@ func shuffle2(a interface{}) interface{} {
func shuffle3(a interface{}) {
val := reflect.ValueOf(a)
tmp := reflect.New(val.Index(0).Type())
l := val.Len()

for i := 0; i < val.Len(); i++ {
j := rand.Intn(i + 1)
for i := 0; i < l; i++ {
j := i + rand.Intn(l-i)
tmp.Elem().Set(val.Index(j))
val.Index(j).Set(val.Index(i))
val.Index(i).Set(tmp.Elem())
Expand Down

0 comments on commit 83d671f

Please sign in to comment.