back

by vardump·12y ago·view on hn ↗
Just tried go1.2.1. Hashmap iteration order was not random, it was same every time. Same happens with Go playground: (http://play.golang.org/p/px7C_lCn6d).

I got this output by running same locally (go1.2.1.darwin-amd64-osx10.8.pkg):

  $ go run test.go 
  There are 500 views for unix
  There are 300 views for python
  There are 100 views for go
  There are 4000 views for javascript
  There are 800 views for testing
  There are 500 views for unix
  There are 300 views for python
  There are 100 views for go
  There are 4000 views for javascript
... same order is repeated forever.
2 comments
The Go playground and tour won't generate random numbers: "Note: the environment in which these programs are executed is deterministic, so rand.Intn will always return the same number."

Could this cause iteration order to be the same when executed there?

Perhaps, but I'm getting analogous results when running exactly same code locally.
The ordering is only quasi-random, depending on how keys are distributed across hash buckets. Your particular example will no longer have a consistent iteration order starting in Go 1.3.

https://code.google.com/p/go/issues/detail?id=6719

So random order in Go1.3. Sounds good to me.

Tried anyways with ten items, the results are interesting:

http://play.golang.org/p/dVZvmBOu_g

With ten items, it seems to randomly (?) alternate between two iteration orders.

Local Go1.2.1 has similar behavior.