Go version

go version 1.23

Output of go env in your module/workspace:

N/A

What did you do?

I wanted to update robpike.io/ivy to math/rand/v2, but was inconvenienced by math/big/Int.Rand using the old random number generator. The type in the signature precludes using v2.

Compatibility of output aside, it should be easy to update, and also a good idea to keep the library consistent. math/rand/v2 is the right one to use.

What did you see happen?

Sadness and pain.

What did you expect to see?

Joy and clear skies.

Comment From: prattmic

math/rand.Rand and math/rand/v2.Rand are different types, so changing the signature of math/big.(*Int).Rand would be an incompatible change.

Is the idea here to add something like math/big.(*Int).RandV2 that takes a math/rand/v2.Rand?

Comment From: randall77

You could use crypto/rand.Int instead, possibly. That would require packing up something from math/rand/v2 as a io.Reader. Chacha8 implements io.Reader, but rand.Rand doesn't? Not sure why that is.

Comment From: robpike

I believe it was to discourage people from just calling rand.Read to generate poor quality passwords.

Comment From: rsc

Using crypto/rand.Int with crypto/rand.Reader seems fine.

Comment From: robpike

One detail I need from ivy, as well as any simulation tool, is that I can seed it, which crypto/rand will not allow.

Comment From: rsc

In that case, passing a seeded math/rand/v2.NewChaCha8 to crypto/rand.Int is probably best.

ChaCha8 implements Reader because as long as the seed is unpredictable, it is strong enough to read arbitrary amounts of bytes without any appreciable patterns or reverse analysis.

The same is not true of PCG, so PCG does not implement Reader.

Comment From: seankhliao

So the suggestion right now is:

bigRand, _ := crypto/rand.Int(math/rand/v2.NewChaCha8(seed), big.NewInt(maximum))