// Copyright 2023 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.package randimport ()// https://numpy.org/devdocs/reference/random/upgrading-pcg64.html// https://github.com/imneme/pcg-cpp/commit/871d0494ee9c9a7b7c43f753e3d8ca47c26f8005// A PCG is a PCG generator with 128 bits of internal state.// A zero PCG is equivalent to NewPCG(0, 0).typePCGstruct { hi uint64 lo uint64}// NewPCG returns a new PCG seeded with the given values.func (, uint64) *PCG {return &PCG{, }}// Seed resets the PCG to behave the same way as NewPCG(seed1, seed2).func ( *PCG) (, uint64) { .hi = .lo = }// AppendBinary implements the [encoding.BinaryAppender] interface.func ( *PCG) ( []byte) ([]byte, error) { = append(, "pcg:"...) = byteorder.BEAppendUint64(, .hi) = byteorder.BEAppendUint64(, .lo)return , nil}// MarshalBinary implements the [encoding.BinaryMarshaler] interface.func ( *PCG) () ([]byte, error) {return .AppendBinary(make([]byte, 0, 20))}var errUnmarshalPCG = errors.New("invalid PCG encoding")// UnmarshalBinary implements the [encoding.BinaryUnmarshaler] interface.func ( *PCG) ( []byte) error {iflen() != 20 || string([:4]) != "pcg:" {returnerrUnmarshalPCG } .hi = byteorder.BEUint64([4:]) .lo = byteorder.BEUint64([4+8:])returnnil}func ( *PCG) () (, uint64) {// https://github.com/imneme/pcg-cpp/blob/428802d1a5/include/pcg_random.hpp#L161 // // Numpy's PCG multiplies by the 64-bit value cheapMul // instead of the 128-bit value used here and in the official PCG code. // This does not seem worthwhile, at least for Go: not having any high // bits in the multiplier reduces the effect of low bits on the highest bits, // and it only saves 1 multiply out of 3. // (On 32-bit systems, it saves 1 out of 6, since Mul64 is doing 4.)const ( = 2549297995355413924 = 4865540595714422341 = 6364136223846793005 = 1442695040888963407 )// state = state * mul + inc , = bits.Mul64(.lo, ) += .hi* + .lo* , := bits.Add64(, , 0) , _ = bits.Add64(, , ) .lo = .hi = return , }// Uint64 return a uniformly-distributed random uint64 value.func ( *PCG) () uint64 { , := .next()// XSL-RR would be // hi, lo := p.next() // return bits.RotateLeft64(lo^hi, -int(hi>>58)) // but Numpy uses DXSM and O'Neill suggests doing the same. // See https://github.com/golang/go/issues/21835#issuecomment-739065688 // and following comments.// DXSM "double xorshift multiply" // https://github.com/imneme/pcg-cpp/blob/428802d1a5/include/pcg_random.hpp#L1015// https://github.com/imneme/pcg-cpp/blob/428802d1a5/include/pcg_random.hpp#L176const = 0xda942042e4dd58b5 ^= >> 32 *= ^= >> 48 *= ( | 1)return}
The pages are generated with Goldsv0.7.9-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.