diff --git a/lang/fastrand/fastrand.go b/lang/fastrand/fastrand.go index 862654c6..58a7cc72 100644 --- a/lang/fastrand/fastrand.go +++ b/lang/fastrand/fastrand.go @@ -129,22 +129,19 @@ func Uint64n(n uint64) uint64 { // It is safe for concurrent use. func Read(p []byte) (int, error) { l := len(p) - if l == 0 { return 0, nil } // Used for local XORSHIFT. - var tmp [2]uint32 - tmp[0], tmp[1] = Uint32(), Uint32() + // Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf + s0, s1 := Uint32(), Uint32() if l >= 4 { var i int uint32p := *(*[]uint32)(unsafe.Pointer(&p)) - s1, s0 := tmp[0], tmp[1] for l >= 4 { // Local XORSHIFT. - // Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf s1 ^= s1 << 17 s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16 s0, s1 = s1, s0 @@ -153,13 +150,10 @@ func Read(p []byte) (int, error) { i++ l -= 4 } - tmp[0], tmp[1] = s1, s0 } if l > 0 { // Local XORSHIFT. - // We don't need to save s0 and s1(tmp is not used). - s1, s0 := tmp[0], tmp[1] s1 ^= s1 << 17 s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16