Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve testing for Animal Magic Exercise #2734

Conversation

tomasnorre
Copy link
Contributor

I had a conversation with my mentor exercise, Animal Magic, about the tests for the exercise.

I have pasted some of my observations/questions, and added an in my eyes, improved and more correct test.

Me:

I have seen multiple community examples like
https://exercism.org/tracks/go/exercises/animal-magic/solutions/ro-jc
https://exercism.org/tracks/go/exercises/animal-magic/solutions/andrerfcsantos

Where they do rand.Intn(20) + 1 but still marks as tests passed. I don't get that. That would in my eyes from time to time result in a die roll of 21, and no only between 1 and 20 as stated in the requirements.

Can someone explain this to me?

Mentor:

"Intn returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n) from the default Source. It panics if n <= 0."

Me:

I read that, also before posting. But doesn't it say between 0 and 100 if rand.Intn(100) or what am I reading/misunderstanding wrong.

Despite that either the two examples i linked to should fail or my should fail, they cannot both be right.

Not my code, this could in my eyes potentially also return 21.

// RollADie returns a random int between 1 and 20
func RollADie() int {
	return 1 + rand.Intn(20)
}

My code:

// RollADie returns a random int d with 1 <= d <= 20.
func RollADie() int {
	return rand.Intn(19) + 1
}

They cannot both be right.

Mentor:

rand.Intn(20) will return integers from range 0 to 19 - note that the 0 in included in the range while 20 it is not. In order to generate an int that can be in range 1 - 20 you need to use 1 + rand.Intn(20) or rand.Intn(20) + 1. The second example will generate an int in range 0-18 and add 1 to the final, returned value. The clue I meantioned is in the range - half-open interval [0,n)

Me:

Thanks for your explanation will check it out tomorrow, and see if I understand it better, or even suggest an improvement to the code base.

So long story short, I think the solutions, my solution, with the return rand.Intn(19) + 1 are incorrect, and my new improved tests will show that. The tests will now "ensure" more than before that all numbers are rolled, still within the scope of 1-20. Of course, there is still a small statics possibility that e.g. 20 is never rolled on 100 rolls.

I can of course be completely wrong as I'm new to go, but would be happy to discuss this, so that we can get an improved code base.

Let me know what you think.

Copy link
Contributor

Hello. Thanks for opening a PR on Exercism 🙂

We ask that all changes to Exercism are discussed on our Community Forum before being opened on GitHub. To enforce this, we automatically close all PRs that are submitted. That doesn't mean your PR is rejected but that we want the initial discussion about it to happen on our forum where a wide range of key contributors across the Exercism ecosystem can weigh in.

You can use this link%20from%20the%20default%20Source.%20It%20panics%20if%20n%20%3C=%200.%22%0D%0A%0D%0AMe:%20%0D%0A%0D%0A%3E%20I%20read%20that,%20also%20before%20posting.%20But%20doesn't%20it%20say%20between%200%20and%20100%20if%20rand.Intn(100)%20or%20what%20am%20I%20reading/misunderstanding%20wrong.%0D%0A%3E%20%0D%0A%3E%20Despite%20that%20either%20the%20two%20examples%20i%20linked%20to%20should%20fail%20or%20my%20should%20fail,%20they%20cannot%20both%20be%20right.%0D%0A%3E%20%0D%0A%3E%20Not%20my%20code,%20this%20could%20in%20my%20eyes%20potentially%20also%20return%2021.%0D%0A%3E%20%0D%0A%3E%20%60%60%60go%0D%0A%3E%20//%20RollADie%20returns%20a%20random%20int%20between%201%20and%2020%0D%0A%3E%20func%20RollADie()%20int%20%7B%0D%0A%3E%20%09return%201%20+%20rand.Intn(20)%0D%0A%3E%20%7D%0D%0A%3E%20%60%60%60%0D%0A%3E%20My%20code:%0D%0A%3E%20%60%60%60go%0D%0A%3E%20//%20RollADie%20returns%20a%20random%20int%20d%20with%201%20%3C=%20d%20%3C=%2020.%0D%0A%3E%20func%20RollADie()%20int%20%7B%0D%0A%3E%20%09return%20rand.Intn(19)%20+%201%0D%0A%3E%20%7D%0D%0A%3E%20%60%60%60%0D%0A%3E%20They%20cannot%20both%20be%20right.%0D%0A%0D%0AMentor:%0D%0A%3E%20rand.Intn(20)%20will%20return%20integers%20from%20range%200%20to%2019%20-%20note%20that%20the%200%20in%20included%20in%20the%20range%20while%2020%20it%20is%20not.%20In%20order%20to%20generate%20an%20int%20that%20can%20be%20in%20range%201%20-%2020%20you%20need%20to%20use%201%20+%20rand.Intn(20)%20or%20rand.Intn(20)%20+%201.%20The%20second%20example%20will%20generate%20an%20int%20in%20range%200-18%20and%20add%201%20to%20the%20final,%20returned%20value.%20The%20clue%20I%20meantioned%20is%20in%20the%20range%20-%20half-open%20interval%20%5B0,n)%0D%0A%0D%0AMe:%20%0D%0A%3E%20Thanks%20for%20your%20explanation%20will%20check%20it%20out%20tomorrow,%20and%20see%20if%20I%20understand%20it%20better,%20or%20even%20suggest%20an%20improvement%20to%20the%20code%20base.%0D%0A%0D%0ASo%20long%20story%20short,%20I%20think%20the%20solutions,%20my%20solution,%20with%20the%20%60return%20rand.Intn(19)%20+%201%60%20are%20incorrect,%20and%20my%20new%20improved%20tests%20will%20show%20that.%20The%20tests%20will%20now%20%22ensure%22%20more%20than%20before%20that%20all%20numbers%20are%20rolled,%20still%20within%20the%20scope%20of%201-20.%20Of%20course,%20there%20is%20still%20a%20small%20statics%20possibility%20that%20e.g.%2020%20is%20never%20rolled%20on%20100%20rolls.%0D%0A%0D%0AI%20can%20of%20course%20be%20completely%20wrong%20as%20I'm%20new%20to%20go,%20but%20would%20be%20happy%20to%20discuss%20this,%20so%20that%20we%20can%20get%20an%20improved%20code%20base.%0D%0A%0D%0ALet%20me%20know%20what%20you%20think.%20%0D%0A&category=go ) to copy this into a new topic on the forum. If we decide the PR is appropriate, we'll reopen it and continue with it, so please don't delete your local branch.

If you're interested in learning more about this auto-responder, please read this blog post.


Note: If this PR has been pre-approved, please link back to this PR on the forum thread and a maintainer or staff member will reopen it.

@github-actions github-actions bot closed this Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant