rand: let pre-go1.9 users use rand
Fixes golang/go#22777
Change-Id: I02457173061aa88ee2b92fac00a350466d7deb1b
Reviewed-on: https://go-review.googlesource.com/78456
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/rand/rng.go b/rand/rng.go
index c0d530e..00c421e 100644
--- a/rand/rng.go
+++ b/rand/rng.go
@@ -4,8 +4,6 @@
package rand
-import "math/bits"
-
// PCGSource is an implementation of a 64-bit permuted congruential
// generator as defined in
//
@@ -44,14 +42,6 @@
pcg.high = seed // TODO: What is right?
}
-// Uint64 returns a pseudo-random 64-bit unsigned integer as a uint64.
-func (pcg *PCGSource) Uint64() uint64 {
- pcg.multiply()
- pcg.add()
- // XOR high and low 64 bits together and rotate right by high 6 bits of state.
- return bits.RotateLeft64(pcg.high^pcg.low, -int(pcg.high>>58))
-}
-
func (pcg *PCGSource) add() {
old := pcg.low
pcg.low += incLow
diff --git a/rand/uint64.go b/rand/uint64.go
new file mode 100644
index 0000000..58761f6
--- /dev/null
+++ b/rand/uint64.go
@@ -0,0 +1,17 @@
+// Copyright 2017 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.
+
+// +build go1.9
+
+package rand
+
+import "math/bits"
+
+// Uint64 returns a pseudo-random 64-bit unsigned integer as a uint64.
+func (pcg *PCGSource) Uint64() uint64 {
+ pcg.multiply()
+ pcg.add()
+ // XOR high and low 64 bits together and rotate right by high 6 bits of state.
+ return bits.RotateLeft64(pcg.high^pcg.low, -int(pcg.high>>58))
+}
diff --git a/rand/uint64_fallback.go b/rand/uint64_fallback.go
new file mode 100644
index 0000000..2e67888
--- /dev/null
+++ b/rand/uint64_fallback.go
@@ -0,0 +1,21 @@
+// Copyright 2017 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.
+
+// +build !go1.9
+
+package rand
+
+// Uint64 returns a pseudo-random 64-bit unsigned integer as a uint64.
+func (pcg *PCGSource) Uint64() uint64 {
+ pcg.multiply()
+ pcg.add()
+ // XOR high and low 64 bits together and rotate right by high 6 bits of state.
+ return rotateLeft(pcg.high^pcg.low, -int(pcg.high>>58))
+}
+
+func rotateLeft(x uint64, k int) uint64 {
+ const n = 64
+ s := uint(k) & (n - 1)
+ return x<<s | x>>(n-s)
+}