If you let randSpaceline take a *rand.Rand then you can control the source of the randomness and arbitrarily set the seed. You also get the advantage of having a non-mutex protected rand source if you don't need it (we do a similar thing and used rand.<Func> if the *rand.Rand is nil as a convenience).
On Tue, 2017-03-14 at 18:50 -0700, Doug Ireton wrote: > I'm a new Gopher and I'm working through "Learn Go" by Nathan > Youngman and > trying to TDD the exercises to learn how to write testable Go code. > > I have a function to return a random spaceline > <https://play.golang.org/p/g5JnrIFyjo> from a string array. > > In Go, how do I test functions which depend on random numbers? And, > yes, I > know that "math/rand" isn't truly random. > > Is it as simple as setting a seed right before I run my test, e.g. > rand.Seed(1)? Do I set rand.Seed(1) at the top of the _test.go file, > or at > the beginning of each unit test? > > Also, am I seeding math.rand correctly in the Init() function? Will > seeding > it in the Init() function override any seeding I do in my tests? > > My only other thought is to create an interface somehow to mock > rand.Intn(), > but this seems like overkill and I don't know enough about interfaces > to > know if this is inadvisable. > -- 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.