I've edited the example very slightly at: https://play.golang.org/p/s7CUSbS8P3I
Let's break this down. Think about how you might shuffle a pack of cards, a simple way is to swap 2 cards around at a time. As long as you exchange enough cards enough times and both cards you chose are randomly picked, eventually you'll have shuffled the pack. Please be happy with that as a concept before going further. The rand.Shuffle therefore doesn't care about what it has to shuffle. All it "thinks" about is: swap element 2 and 7, swap element 24 and 9 etc. (the numbers aren't important here, just that anything in an array can be shuffled by giving these sorts of instructions). All it needs to know is how long the array it has to shuffle is, and "who" can swap things around. I'll repeat that as it might not be obvious. It doesn't have to know how to swap things, it doesn;t have to know anything about the things. If I told you to shuffle 52 cards by shouting out numbers, the exact same instruction if you were instead shuffling 52 cars, or airplanes, or dogs; it's the number of things in the list and the numbers given as instructions that matter. It therefore only has to know of a function that can do this swap around. So we also pass it what I have called swapper. Who is an agent who you say, swap 2 and 7, 24 and 9, etc and it will do that. So it can call swapper with i and j set to two values and swapper does the swap. So how does this play into go: Well we've declared the initial array as an array of words. We've now declared swapper as someone who can carry out the function of swapping any 2 items in that array, that is the item at location i gets put at the position j was and the item at location j gets put where i was. I explicitly declared swapper as a func that takes two integers as that's the only thing rand.Shuffle knows or cars about the function. It just calls it a whole bunch of times, doesn't pay attention to anything it does, it just trusts it to do what needs to be done. So as far as rand.Shuffle goes, it needs how many items are in the array, and a function that it can call with two integer parameters. so we sat to rand.shuffle what the length of the array is, and to use swapper. And that's it. Don't worry about how rand.Shuffle works (until you want to). Yes it's a good bit of computer science to understand how it works, but it's also good computer science to accept that you don't need to know how things work under the hood either. If that overly wordy version helps? Chris On Monday, 15 October 2018 15:06:58 UTC+1, Neil Higgins wrote: > > *Here's the doc for shuffle in math/random*: > > func Shuffle(n int <https://golang.org/pkg/builtin/#int>, swap func(i, j int > <https://golang.org/pkg/builtin/#int>)) > > Shuffle pseudo-randomizes the order of elements using the default Source. > n is the number of elements. Shuffle panics if n < 0. swap swaps the > elements with indexes i and j. > > *And here's the example*: > > package main > > import ( > "fmt" > "math/rand" > "strings" > ) > > func main() { > words := strings.Fields("ink runs from the corners of my mouth") > rand.Shuffle(len(words), func(i, j int) { > words[i], words[j] = words[j], words[i] > }) > fmt.Println(words) > > } > > *Why can't I understand a word of it?* > (This is not atypical, by the way - and not just for go - so if someone > can educate me, I might suddenly start prospering in the software game) > > Issues (for me): > > - Shuffle doesn't seem to swap anything (the thing you want shuffled > isn't even an argument) > - As I read it the example, it should only swap two words (index i and > j) but it seems to shuffle the everything > - What's this bloody "func" thing??? > - Why doesn't "swap" appear in the example? > > Yours sincerely, > *Confused* > -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.