First thanks to anybody that spends the time to read this.
I’m new to Go and just found two rand packages, and got fascinated by the differences.
This discussion is extremely enlightening, but for those of you who are new to Go, like me, it might be interesting to really see the randomness of the random number generators in action.
The little program I wrote is basically my try to understand Rob Pikes fan in pattern.
Rob Pike’s code here: https://play.golang.org/p/buy30qw5MM
I wrote a similar program where a generator generates random numbers between 1 and 6 and so simulates a dice roll.
Two of these generators are launched concurrently and then a collector goroutine adds up the tally of each of the generators.
This example uses math/rand:
And this example uses crypto/rand:
The first example has maybe 2 possible outcomes across all 10 rolls, usually just 1!
The second is more in line with what I’d expect from dice rolls. Sometimes A wins, sometimes B wins.
So, what I learned is: Whenever I want numbers to be random, for any games e.g., I need to use crypto/rand.
Am I getting this wrong?
Any comments on the code also greatly welcome.