math/rand is Go’s standard-library package for generating pseudorandom values in small, noncritical applications. Its Intn(n) function returns an integer from 0 through n-1. section 5.3.3
Choose the random-number package according to the consequence of predictability:
- Use math/rand for ordinary application choices, such as selecting a word from a corpus. section 5.3.3
- Use crypto/rand for passwords, tokens, and other security-related objects; it costs more because it gathers system-level entropy. section 5.3.3
- Treat the generator’s source as replaceable when controlled randomness is needed. Programs before Go 1.20 required rand.Seed; newer versions seed randomly at startup. section 5.3.3
Selecting and testing a corpus word
A random index makes word selection direct: index := rand.Intn(len(corpus)); return corpus[index]. Test the invariant rather than expecting a particular word: verify that the result occurs in the supplied corpus.
