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

Fix random bugs #6

Merged
merged 4 commits into from
Dec 1, 2018
Merged

Fix random bugs #6

merged 4 commits into from
Dec 1, 2018

Conversation

robyoder
Copy link
Contributor

Fixes #5

  1. Don't generate 8 random bytes when we only need 4.
  2. Fix performance and non-uniform bug in int31n.

Thanks, @Sc00bz!

@Sc00bz
Copy link
Contributor

Sc00bz commented Nov 30, 2018

It should probably still panic if n is zero since "[0,n)" is not a valid range. Also "Based on Int32n from the math/rand package.." there is no Int32n it's Int31n.

@robyoder
Copy link
Contributor Author

@Sc00bz it's not a valid range, but I don't think the panic is useful.

@Sc00bz
Copy link
Contributor

Sc00bz commented Nov 30, 2018

Well setting n to zero is equivalent to a divide by zero which is a panic. Is there an equivalent in Go to C/C++'s assert() that are removed when not debugging? This way if there is weird behavior you can get this error when debugging.

Copy link
Contributor

@jpgoldberg jpgoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The two fixes and the name cleanup are good things.

I do want consider what the proper behavior should be on a division by zero, but let's make that a separate issue.

b := make([]byte, 8)
// randomUint32 creates a random 32 bit unsigned integer
func randomUint32() uint32 {
b := make([]byte, 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'm not sure why I originally had 8 in there. Probably because I can't do arithmetic.

// Based on Int31n from the math/rand package..
func randomUint32n(n uint32) uint32 {
if n <= 1 {
return 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we need to figure out more carefully what we want to happen in this case. This choice is fine for now, but we should create a separate issue for this.

@jpgoldberg jpgoldberg merged commit 3e0f92b into master Dec 1, 2018
@jpgoldberg jpgoldberg deleted the ry/random-bugs branch December 1, 2018 16:30
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.

None yet

3 participants