Re: [go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-16 Thread Simon Ritchie
As some posters have already said, it depends what you are trying to test. If you want to test what your solution does when it receives a randomly-generated number, then you don't need to use random numbers. You can hide the generation of the random number in a type which is defined by an

Re: [go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-16 Thread Michael Jones
You could also capture a (p)random stream as a test data vector and use it in lieu of the PRNG during tests. This is the same as seeding iff and only iff the PRNG is never changed, Should that change, then so will the tests unless you capture a test vector now. (For example, if you have a timing

[go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-15 Thread Robert Johnstone
If you are fixing the seed, you are probably over constraining your test. Even though you are calling rand, there must be some post-conditions that the function is supposed to guarantee. In this case, you probably expect all options to be returned with equal probability, so you should call

[go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-15 Thread mhhcbon
Hi, if you wish not declare a struct and its interface, you might simply make a function type. See https://play.golang.org/p/xuycrPc8sF Then in your main program, you consume the func type, and inject a func impl. It is not as versatile as interface, but that can make the job. -- You

[go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-15 Thread xiiophen
> > > 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? > > You're using time.Now().UTC().UnixNano()) but time.Now().UnixNano() or time.Now().Unix() seem just as good Note UnixNano() may be

[go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-15 Thread xiiophen
https://golang.org/pkg/math/rand/#Seed *"Seed uses the provided seed value to initialize the default Source to a deterministic state. If Seed is not called, the generator behaves as if seeded by Seed(1). "* So even without calling rand.Seed your output will be the same everytime you run

[go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-15 Thread Egon
On Wednesday, 15 March 2017 05:45:22 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 >

[go-nuts] Re: How do I test an func which depends on (pseudo)-random numbers, e.g. rand.Intn()?

2017-03-14 Thread Miki Tebeka
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 >