diff --git a/doc/next/6-stdlib/99-minor/math/rand/v2/77853.md b/doc/next/6-stdlib/99-minor/math/rand/v2/77853.md new file mode 100644 index 0000000000..163dcd1527 --- /dev/null +++ b/doc/next/6-stdlib/99-minor/math/rand/v2/77853.md @@ -0,0 +1 @@ +add the generic method [*Rand].N, matching the behavior of the top-level N function. \ No newline at end of file diff --git a/src/math/rand/v2/n.go b/src/math/rand/v2/n.go new file mode 100644 index 0000000000..d1ee16abd0 --- /dev/null +++ b/src/math/rand/v2/n.go @@ -0,0 +1,21 @@ +//go:build goexperiment.genericmethods + +// Copyright 2026 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +package rand + +// N returns a pseudo-random number in the half-open interval [0,n). +// The type parameter Int can be any integer type. +// It panics if n <= 0. +func (r *Rand) N[Int intType](n Int) Int { + // TODO: See CL 775100, + // When delete the goexperiment.genericmethods, + // make this method + // share the implementation with the global function N + // and delete this file. + if n <= 0 { + panic("invalid argument to N") + } + return Int(r.uint64n(uint64(n))) +}