IMO the right approach is as you suggested - seed once in init. init is called before your tests, so the seed you do in your test will override it.
On Wednesday, March 15, 2017 at 5:45:22 AM UTC+2, 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.