Incomplete test for RollADie() in Animal Magic exercise

When comparing my solution to other submissions, I have noticed a discrepancy in the answers, which led me to believe that the test doesn’t assure correct understanding and implementation of rand.Intn() boundary.

Accepted solutions would include anything with a value under 20, which is not what we want. An example of a wrong solution: Rlyehan's solution for Animal Magic in Go on Exercism

I would suggest updating the test to something like this, but I am not 100% sure how to achieve determinism other than by increasing the number of tests. I am sure there is a way of doing that in go, but I am too new to it.

func TestRollADie(t *testing.T) {
	const tests = 100

	seen := make(map[int]bool)

	for i := 0; i < tests; i++ {
		got := RollADie()
		if got < 1 || got > 20 {
			t.Fatalf("RollADie() out of range: %d", got)
		}
		seen[got] = true
	}

	if len(seen) != 20 {
		t.Errorf("Not all sides of the die appeared. Seen sides: %v", seen)
	}
}

Thanks for bringing this up. But there is no way to bring determinism to random things - not in Go, not in any other language.

We decided to keep this discussion for the mentoring sessions. Testing doesn’t fix anything here but makes it harder for students to understand what is expected. E.g. to pass your test it is enough to return a global counter increased with every call and bounded into the allowed set of numbers. No randomness required at all.

References:
Improved test for C# roll_the_die, roll-the-dice doesn’t reinforce the randomness concept, Robot Name exercise doesn’t test randomness (at least in Go) and there’s probably a bunch more in the forum.

3 Likes