Shuffle
Opened this issue · 0 comments
robotamer commented
Just found this package and think its great, thanks.
However Shuffle() returns always the same sequence.
Below is my test code.
I also included a shuffle of my own.
package main
import (
"fmt"
"github.com/feyeleanor/slices"
)
var is slices.ISlice
func main() {
is.Append(4)
is.Append(3)
is.Append(1)
is.Append(2)
is.Append(3)
slices.Sort(is)
slices.Shuffle(is)
fmt.Println(is)
}
import (
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
// Shuffles the string
func Shuffle(s string) string {
l := len(s)
b := []byte(s)
for i := len(b) - 1; i != 0; i-- {
r := rand.Intn(l)
b[i], b[r] = b[r], b[i]
}
return string(b)
}