Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shuffle #1

Open
robotamer opened this issue Aug 28, 2013 · 0 comments
Open

Shuffle #1

robotamer opened this issue Aug 28, 2013 · 0 comments

Comments

@robotamer
Copy link

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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant