var res []Item
//fill res logic
shuffle := make(map[int]*Item)
for k, v := range res {
shuffle[k] = &v
}
res = nil
for _, v := range shuffle {
res = append(res, *v)
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
-j
While iteration over a map is said to be random, it isn't specified
exactly how "random" the iteration is. The spec says
https://golang.org/ref/spec#RangeClause
The iteration order over maps is not specified and is not guaranteed
to be the same from one iteration to the next.
That is, the iteration order should not to be relied upon. A simple test like
https://play.golang.org/p/czRE3pbMzc
shows this: the keys are printed in a scrambled order. When I run this
on my machine, I get a different order every time, but on the
playground the order seems to be fixed and I get "0 5 7 1 2 3 4 6 8 9"
every time. My guess would be that an external input is used to
initialize the hash function used behind the scene -- and on the
playground that input somehow has a fixed value.
Unless you're build a toy application, my advice would be to use a
real random generator to generate indexes into the map. Use these
indexes to swap elements and permute the array as Konstantin mentioned
(https://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
--
Martin Geisler
Thanks for the reference! BTW I love this behaviour!
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/UIYHKFeIf_k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.