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

[chore] Migrate from math/rand to math/rand/v2 #34685

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

webstradev
Copy link

Description:

Migrating from math/rand to math/rand/v2.

Largely involves the following changes:

  • Replaced any usage of math/rand.Read with crypto/rand.Read This was not carried over to the v2 API and the suggestion is to use the method from crypto.
  • Removed any source creation / seeding -> No longer needed in math/rand/v2
  • Applied renames for all outdated methods (lowercase n to uppercase N, and 31->32 and 63->64)

NOTE: There is one file where we now import both crypto/rand and math/rand/v2. I decided to alias both of them to avoid any confusion. So the imports in this package are

import (
    cryptoRand "crypto/rand"
    mathRand "math/rand"
)

I thought this would be best because having only one aliased could still be mistakenly read.

Link to tracking Issue: #34676

Testing:
Running make test

Documentation:

@webstradev
Copy link
Author

still some work todo on this one will draft for now

@webstradev webstradev marked this pull request as draft August 14, 2024 16:57
@webstradev
Copy link
Author

@mx-psi Two questions.

  1. Seeding (like is done in countless places with the current time) is no longer necessary in v2, so I have removed it in those cases, but in the cases where we seed with a specific number I'm presuming we want a reproducible result, so in those cases I have gone with a different way of creating a source, because rand.NewSource does not exist in v2. So I've used rand.New(rand.NewPCG(seed, seed)) is that okay?
  2. v2 removes the .Read implementation and suggests using crypto/rand.Read for true randomness, so I've done that for one case, but there are two cases where we seeded it with a specific seed (for reproducibility) as well. So not entirely sure how to proceed with those cases. I could alternatively leave those two packages out of this MR and that would leave only two packages on rand/v1

Thoughts?

@mx-psi
Copy link
Member

mx-psi commented Aug 20, 2024

@webstradev

So I've used rand.New(rand.NewPCG(seed, seed)) is that okay?

Yeah that seems okay, from https://go.dev/blog/chacha8rand#the-pcg-generator it seems like this is intended as the new default generator.

there are two cases where we seeded it with a specific seed (for reproducibility) as well. So not entirely sure how to proceed with those cases. I could alternatively leave those two packages out of this MR and that would leave only two packages on rand/v1

The godoc for rand.Read says:

If a deterministic source is required, use math/rand/v2.ChaCha8.Read.

Why don't we use this?

@webstradev
Copy link
Author

webstradev commented Aug 20, 2024

@webstradev

So I've used rand.New(rand.NewPCG(seed, seed)) is that okay?

Yeah that seems okay, from https://go.dev/blog/chacha8rand#the-pcg-generator it seems like this is intended as the new default generator.

there are two cases where we seeded it with a specific seed (for reproducibility) as well. So not entirely sure how to proceed with those cases. I could alternatively leave those two packages out of this MR and that would leave only two packages on rand/v1

The godoc for rand.Read says:

If a deterministic source is required, use math/rand/v2.ChaCha8.Read.

Why don't we use this?

Yeah we can do this now, it was only added in 1.23 so when I was first looking at it it wasn't an option.
I'll see if I can sort this, however it does mean we need to update to 1.23, not sure if that is in scope for this PR? @mx-psi

Signed-off-by: Erik Westra <e.s.westra.95@gmail.com>
@mx-psi
Copy link
Member

mx-psi commented Aug 20, 2024

Yeah we can do this now, it was only added in 1.23 so when I was first looking at it it wasn't an option.

Ah, I didn't realize this was Go 1.23 only 😞 I guess we can either:

  • use crypto for it (not sure if this is feasible?) OR
  • file an issue for tackling this once we support Go 1.23 and exclude these files from the linter for now

@webstradev
Copy link
Author

Yeah we can do this now, it was only added in 1.23 so when I was first looking at it it wasn't an option.

Ah, I didn't realize this was Go 1.23 only 😞 I guess we can either:

  • use crypto for it (not sure if this is feasible?) OR
  • file an issue for tackling this once we support Go 1.23 and exclude these files from the linter for now

I think I'll go for the second option if that is okay. Do you usually mark a todo in code and reference an issue number as well

@mx-psi
Copy link
Member

mx-psi commented Aug 20, 2024

Do you usually mark a todo in code and reference an issue number as well

Yes, that's the usual way of doing it: put a TODO comment with a link to the issue

Signed-off-by: Erik Westra <e.s.westra.95@gmail.com>
@webstradev webstradev marked this pull request as ready for review August 20, 2024 16:31
@mx-psi mx-psi mentioned this pull request Aug 22, 2024
25 tasks
@mx-psi mx-psi linked an issue Aug 22, 2024 that may be closed by this pull request
25 tasks
Copy link
Contributor

github-actions bot commented Sep 4, 2024

This PR was marked stale due to lack of activity. It will be closed in 14 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate to math/rand/v2
4 participants