Skip to content

Instantly share code, notes, and snippets.

@alphazero
Created May 18, 2015 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphazero/dd0e432683ed2410998e to your computer and use it in GitHub Desktop.
Save alphazero/dd0e432683ed2410998e to your computer and use it in GitHub Desktop.
Xorshift* 1024 (essential fragment)
var s0 = [16]uint64{
0xEA221D1E5C8DB483,
0xF89369282348A220,
0x7022326276090608,
0x1618FCC12E583E30,
0xF7E7C005F85EFC69,
0x132B746F9C2FF047,
0x338324A69CBDC6B5,
0x2B91B21FAAB58FE0,
0x85CB192105B8B12B,
0x5A9EC4772C1B642D,
0xFEA936CB4A0CEA20,
0x6865516844ED9BBD,
0x16C12E5DCC3D7365,
0x0BBDE3B5363FA6E9,
0xD454C1D29E450885,
0x480EB817028DB197,
}
type xorshiftstar struct {
s [16]uint64
p uint8
}
// global instance
var (
s = s0 // copy reference init state.
xss = &xorshiftstar{s, 0} // global instance uses s
)
/// algorithm /////////////////////////////////////////////////////////////
// NOTICE-BEGIN - original source: Xorshift - wikipedia.org. adapted for Go.
const xss_magic = uint64(1181783497276652981)
const (
shift_A = 31 // 23
shift_B = 11 // 17
shift_C = 30 // 30
)
func (s *xorshiftstar) next() uint64 {
var s0 = s.s[s.p]
s.p = (s.p + 1) & 0xF
var s1 = s.s[s.p]
s1 ^= s1 << shift_A
s1 ^= s1 >> shift_B
s0 ^= s0 >> shift_C
// update state
s.s[s.p] = s0 ^ s1
return s.s[s.p] * xss_magic
}
// NOTICE-END - original source: Xorshift - wikipedia.org. adapted for Go.
/// api ///////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment