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: Perm not working #20971

Closed
liamg opened this issue Jul 10, 2017 · 1 comment
Closed

math/rand: Perm not working #20971

liamg opened this issue Jul 10, 2017 · 1 comment

Comments

@liamg
Copy link

liamg commented Jul 10, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.3 linux/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/lgalvin/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build653200321=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

Tried to use Perm to retrieve random values.

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	for _, value := range rand.Perm(3) {
		fmt.Println(value)
	}

	// Unordered output: 1
	// 2
	// 0
}

always returns:

0
1
2

The values are not randomised.

Run it here: https://play.golang.org/p/gOAw978ZNI

What did you expect to see?

Randomised values.

What did you see instead?

Same order each time:

0
1
2
@ALTree
Copy link
Member

ALTree commented Jul 10, 2017

Same order each time

This is expected: Perm is pseudorandom, the output is deterministic if you perform the same call with the same randomness source. If you want the permutation to change you'll have to call Perm from a custom source using func (r *Rand) Perm(n int) []int.

Closing this, since it's not a bug.

@ALTree ALTree closed this as completed Jul 10, 2017
@mikioh mikioh changed the title rand.Perm not working math/rand: Perm not working Jul 21, 2017
@golang golang locked and limited conversation to collaborators Jul 21, 2018
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

3 participants