math/rand/v2: add Uint

Uint was part of the approved proposal but was inadvertently left
out of Go 1.22. Add for Go 1.23.

Change-Id: Ifaf24447bd70c8524c2fd299eefdf4aa29e49e66
Reviewed-on: https://go-review.googlesource.com/c/go/+/583455
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Russ Cox 2024-05-06 09:05:24 -04:00
parent 27ed85d4d1
commit 9c4849bf20
4 changed files with 38 additions and 1 deletions

2
api/next/61716.txt Normal file
View file

@ -0,0 +1,2 @@
pkg math/rand/v2, func Uint() uint #61716
pkg math/rand/v2, method (*Rand) Uint() uint #61716

View file

@ -0,0 +1,2 @@
The [Uint] function and [Rand.Uint] method have been added.
They were inadvertently left out of Go 1.22.

View file

@ -56,6 +56,9 @@ func (r *Rand) Int32() int32 { return int32(r.src.Uint64() >> 33) }
// Int returns a non-negative pseudo-random int.
func (r *Rand) Int() int { return int(uint(r.src.Uint64()) << 1 >> 1) }
// Uint returns a pseudo-random uint.
func (r *Rand) Uint() uint { return uint(r.src.Uint64()) }
// Int64N returns, as an int64, a non-negative pseudo-random number in the half-open interval [0,n).
// It panics if n <= 0.
func (r *Rand) Int64N(n int64) int64 {
@ -291,6 +294,9 @@ func Int32() int32 { return globalRand.Int32() }
// Int returns a non-negative pseudo-random int from the default Source.
func Int() int { return globalRand.Int() }
// Uint returns a pseudo-random uint from the default Source.
func Uint() uint { return globalRand.Uint() }
// Int64N returns, as an int64, a pseudo-random number in the half-open interval [0,n)
// from the default Source.
// It panics if n <= 0.

View file

@ -80,6 +80,9 @@ func TestRegress(t *testing.T) {
x = int(big)
case reflect.Uint:
if m.Name == "Uint" {
continue
}
big := uint64s[repeat%len(uint64s)]
if uint64(uint(big)) != big {
r.Uint64N(big) // what would happen on 64-bit machine, to keep stream in sync
@ -118,7 +121,7 @@ func TestRegress(t *testing.T) {
if *update {
var val string
big := int64(1 << 60)
if int64(int(big)) != big && (m.Name == "Int" || m.Name == "IntN") {
if int64(int(big)) != big && (m.Name == "Int" || m.Name == "IntN" || m.Name == "Uint" || m.Name == "UintN") {
// 32-bit machine cannot print 64-bit results
val = "truncated"
} else if reflect.TypeOf(out).Kind() == reflect.Slice {
@ -134,6 +137,9 @@ func TestRegress(t *testing.T) {
if m.Name == "Int" {
want = int64(int(uint(want.(int64)) << 1 >> 1))
}
if m.Name == "Uint" {
want = uint64(uint(want.(uint64)))
}
if !reflect.DeepEqual(out, want) {
t.Errorf("r.%s(%s) = %v, want %v", m.Name, argstr, out, want)
}
@ -456,6 +462,27 @@ var regressGolden = []any{
[]int{1, 3, 4, 5, 0, 2, 7, 8, 6}, // Perm(9)
[]int{1, 8, 4, 7, 2, 6, 5, 9, 0, 3}, // Perm(10)
uint64(14192431797130687760), // Uint()
uint64(11371241257079532652), // Uint()
uint64(14470142590855381128), // Uint()
uint64(14694613213362438554), // Uint()
uint64(4321634407747778896), // Uint()
uint64(760102831717374652), // Uint()
uint64(9221744211007427193), // Uint()
uint64(8289669384274456462), // Uint()
uint64(2449715415482412441), // Uint()
uint64(3389241988064777392), // Uint()
uint64(12210202232702069999), // Uint()
uint64(8204908297817606218), // Uint()
uint64(17358349022401942459), // Uint()
uint64(2240328155279531677), // Uint()
uint64(7311121042813227358), // Uint()
uint64(14454429957748299131), // Uint()
uint64(13481244625344276711), // Uint()
uint64(9381769212557126946), // Uint()
uint64(1350674201389090105), // Uint()
uint64(6093522341581845358), // Uint()
uint32(3304433030), // Uint32()
uint32(2647573421), // Uint32()
uint32(3369092613), // Uint32()