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: not random. #21771
Comments
[jkayser@oel7latest exercise7.3]$ cat treesort_test.go package treesort_test import (
) func TestSort(t *testing.T) {
} [jkayser@oel7latest exercise7.3]$ |
[jkayser@oel7latest exercise7.3]$ cat src/treesort/treesort.go // See page 101. // Package treesort provides insertion sort using an unbalanced binary tree. import ( //!+ // Sort sorts values in place. func GetTree(values []int) (*Tree) { // appendValues appends the elements of t to values in order func add(t *Tree, value int) *Tree { //!- func (t *Tree) String() string { |
[jkayser@oel7latest exercise7.3]$ cat run.sh GOPATH=" [jkayser@oel7latest exercise7.3]$ ./run.sh |
This is working as intended. There is a difference between deterministic and non-deterministic pseudo-randomness. The sequence of values is pseudo-random, but it is deterministically generated unless you provide a seed: rand.Seed(time.Now().UnixNano()) |
Why doesn't it initialized with a seed? |
There is value in having deterministic pseudo-randomness and the need for non-determinism is trivially done with the one line shown above. |
As you noticed, it also makes it more clear that these are deterministic, which hopefully avoids people mistakingly using them for cryptographic purposes. |
Deterministic is a big word. Why not just call it "not random"? So, you can correctly say that the math rand function returns a "not random" (deterministic) number if a seed has not been supplied. Geeze, throw the poor developer a bone, and add a basic seed initialization call to the package init() function. func init() { That would be better than nothing. Or, if you really don't want to do that, have rand return an error if the seed has not been supplied: if !seeded { Why even have a rand function that isn't cryptographically secure? Eliminate it. That way, developers who want randomness will not be able to get unrandomness under any circumstance. |
Better yet, have math rand just call crypto rand, and eliminate the caveat about being deterministic. |
If the authors of the original code (Alan A. A. Donovan & Brian W. Kernighan) can forget to call the Seed function before calling Rand, what is the likelihood that lesser developers will make the same mistake? |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?[jkayser@oel7latest exercise7.3]$ go version
go version go1.9 linux/amd64
[jkayser@oel7latest exercise7.3]$
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?[jkayser@oel7latest exercise7.3]$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jkayser"
GORACE=""
GOROOT="/usr/local/go1.9"
GOTOOLDIR="/usr/local/go1.9/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build505664125=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
[jkayser@oel7latest exercise7.3]$
What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
I was doing Exercise 7.3 in the Go Programming Language book. It has you modify the code from section 4.4 (treesort). When I added code to dump the tree structure (prior to sorting) it dumps the same structure every time. I think the structure is supposed to be randomly populated, but the math.random is generating the same random numbers every time.
https://github.com/adonovan/gopl.io/blob/master/ch4/treesort/sort_test.go
What did you expect to see?
Somewhat ramdomness in the generated numbers, even if they were not cryptographically secure.
What did you see instead?
The same random numbers generated every time.
The text was updated successfully, but these errors were encountered: