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

math/rand: document whether NewSource always implements Source64 #44488

Open
dsnet opened this issue Feb 22, 2021 · 3 comments
Open

math/rand: document whether NewSource always implements Source64 #44488

dsnet opened this issue Feb 22, 2021 · 3 comments

Comments

@dsnet
Copy link
Member

@dsnet dsnet commented Feb 22, 2021

The concrete type returned by NewSource happens to implement Source64. However, this is not documented in any way. Is this always guaranteed?

@ulikunitz
Copy link
Contributor

@ulikunitz ulikunitz commented Feb 22, 2021

I looked into the source code of rng.go and since Int63() calls Uint64(), NewSource is always returning a Source64 implementation.

// Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
func (rng *rngSource) Int63() int64 {
	return int64(rng.Uint64() & rngMask)
}
@dsnet
Copy link
Member Author

@dsnet dsnet commented Feb 22, 2021

I believe the snippet you actually wanted to reference is:

go/src/math/rand/rng.go

Lines 237 to 252 in 87e984a

// Uint64 returns a non-negative pseudo-random 64-bit integer as an uint64.
func (rng *rngSource) Uint64() uint64 {
rng.tap--
if rng.tap < 0 {
rng.tap += rngLen
}
rng.feed--
if rng.feed < 0 {
rng.feed += rngLen
}
x := rng.vec[rng.feed] + rng.vec[rng.tap]
rng.vec[rng.feed] = x
return uint64(x)
}

I'm aware that it current returns something that implements Source64 and has done so for many years. The question is whether this should be a documented guarantee (which I think it should be).

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Feb 23, 2021

I think we should document it.

@cagedmantis cagedmantis added this to the Backlog milestone Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants