Last active
February 25, 2018 20:02
-
-
Save resuna/a03f587676e1e8ba0d0705f401f0faea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org | |
// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website) | |
// Converted to Swift by Peter da Silva, backing out some C optimizations | |
// and hardcoding Knuth's LCG parameters | |
class pcg32_random { | |
var state: UInt64; | |
init(seed: UInt64 = 0) | |
{ | |
state = seed | |
} | |
func next() -> UInt32 | |
{ | |
// Advance internal state, Knuth's mixed LCG | |
state = state &* 6364136223846793005 &+ 1442695040888963407; | |
// Calculate output function (XSH RR) | |
let xorshifted = UInt32(truncatingBitPattern: ((state >> 18) ^ state) >> 27); | |
let rot = UInt32(truncatingBitPattern: state >> 59); | |
return (xorshifted >> rot) | (xorshifted << UInt32((-Int32(rot)) & 31)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a correct version and tested it against the C library: https://gist.github.com/irskep/3a5d6cd75f4a384afff01cf742acf5be