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

math/rand: rand package shuffle won't be pass-by-value #51579

Closed
ghost opened this issue Mar 10, 2022 · 2 comments
Closed

math/rand: rand package shuffle won't be pass-by-value #51579

ghost opened this issue Mar 10, 2022 · 2 comments

Comments

@ghost
Copy link

ghost commented Mar 10, 2022

What version of Go are you using (go version)?

$ go version
go version go1.17.7 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\仮\AppData\Local\go-build
set GOENV=C:\Users\仮\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\仮\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\仮\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.7
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\仮\AppData\Local\Temp\go-build1844132378=/tmp/go-build -gno-record-gcc-switches

What did you do?

doesnt work: https://go.dev/play/p/CXHEZ55IPzg

This works: https://go.dev/play/p/eeODcV4G03t

What did you expect to see?

func main() {
	var list = []string{"1", "2", "3", "4", "5"}

	fmt.Println(shuffleStrings(list)) // [1 5 4 2 3]
	fmt.Println(list) // [1 2 3 4 5]
}
func shuffleStrings(input []string) []string {
	if len(input) == 0 {
		return input
	}

	var copy []string
	for _, v := range input {
		copy = append(copy, v)
	}
	rand.Seed(23443435435423)
	rand.Shuffle(len(copy), func(i, j int) { copy[i], copy[j] = copy[j], copy[i] })
	return copy
}

This is expected behavior

What did you see instead?

func main() {
	var list = []string{"1", "2", "3", "4", "5"}

	fmt.Println(shuffleStrings(list)) // result: [5 3 1 2 4]
	fmt.Println(list) // result: [5 3 1 2 4]
}

func shuffleStrings(input []string) []string {
	rand.Seed(34534543342)
	rand.Shuffle(len(input), func(i, j int) { input[i], input[j] = input[j], input[i] })
	return input
}

Why do shuffleStrings() function change original array
Not pass-by-value

@ianlancetaylor
Copy link
Contributor

rand.Shuffle doesn't change anything itself. It is the swap function that is passed in that determines what happens. In your "doesn't work" case your swap function changes the original slice. In your "this works" case you are making a copy of the original slice, and shuffling that.

You may want to read https://go.dev/blog/slices-intro for more about slices.

Closing because this is working as expected.

@ghost
Copy link
Author

ghost commented Mar 10, 2022

oh ok thanks

@golang golang locked and limited conversation to collaborators Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants