math/rand/v2: add method Rand.N

Derived from CL 775100 by qiu laidongfeng <2645477756@qq.com>

Fixes #77853

Change-Id: Iefcdbd1a7dbf82e732dad9bbf94722878c82c1a6
Reviewed-on: https://go-review.googlesource.com/c/go/+/780582
Reviewed-by: qiu laidongfeng <2645477756@qq.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Austin Clements <austin@google.com>
This commit is contained in:
qiulaidongfeng 2026-05-07 15:27:05 +08:00 committed by Mark Freeman
parent 8621461b26
commit e0a8616941
2 changed files with 22 additions and 0 deletions

View file

@ -0,0 +1 @@
add the generic method [*Rand].N, matching the behavior of the top-level N function.

21
src/math/rand/v2/n.go Normal file
View file

@ -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)))
}