From: Russ Cox Date: Mon, 6 May 2024 13:05:24 +0000 (-0400) Subject: math/rand/v2: add Uint X-Git-Tag: go1.23rc1~445 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9c4849bf20e0f9d4c878318bd358b0911a9fb122;p=gostls13.git 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 Reviewed-by: Robert Griesemer --- diff --git a/api/next/61716.txt b/api/next/61716.txt new file mode 100644 index 0000000000..524ca4b8a5 --- /dev/null +++ b/api/next/61716.txt @@ -0,0 +1,2 @@ +pkg math/rand/v2, func Uint() uint #61716 +pkg math/rand/v2, method (*Rand) Uint() uint #61716 diff --git a/doc/next/6-stdlib/99-minor/math/rand/v2/61716.md b/doc/next/6-stdlib/99-minor/math/rand/v2/61716.md new file mode 100644 index 0000000000..68ff614ee5 --- /dev/null +++ b/doc/next/6-stdlib/99-minor/math/rand/v2/61716.md @@ -0,0 +1,2 @@ +The [Uint] function and [Rand.Uint] method have been added. +They were inadvertently left out of Go 1.22. diff --git a/src/math/rand/v2/rand.go b/src/math/rand/v2/rand.go index 17e55830c8..fdd770058a 100644 --- a/src/math/rand/v2/rand.go +++ b/src/math/rand/v2/rand.go @@ -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. diff --git a/src/math/rand/v2/regress_test.go b/src/math/rand/v2/regress_test.go index c85d58408d..7f2acac21b 100644 --- a/src/math/rand/v2/regress_test.go +++ b/src/math/rand/v2/regress_test.go @@ -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()