]> Cypherpunks repositories - gostls13.git/commitdiff
math/rand/v2: add Uint
authorRuss Cox <rsc@golang.org>
Mon, 6 May 2024 13:05:24 +0000 (09:05 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 7 May 2024 18:03:11 +0000 (18:03 +0000)
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>
api/next/61716.txt [new file with mode: 0644]
doc/next/6-stdlib/99-minor/math/rand/v2/61716.md [new file with mode: 0644]
src/math/rand/v2/rand.go
src/math/rand/v2/regress_test.go

diff --git a/api/next/61716.txt b/api/next/61716.txt
new file mode 100644 (file)
index 0000000..524ca4b
--- /dev/null
@@ -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 (file)
index 0000000..68ff614
--- /dev/null
@@ -0,0 +1,2 @@
+The [Uint] function and [Rand.Uint] method have been added.
+They were inadvertently left out of Go 1.22.
index 17e55830c8f2df7318ed7196869cc031f61a8736..fdd770058a4d78a91fcd4dcf36c5cbe6559a5a97 100644 (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.
index c85d58408dde53864482dfa26da5f41a568b4fdf..7f2acac21b0615b0a87bd75b63cb3a9523269753 100644 (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()