I'm a big fan of the PCG32 PRNG. It's simple, fast, and well documented. As is usually the case when I find interesting code I try and find a way to implement it in Perl. PCG32 poses an interesting problem when one tries to implement it in Perl because all the math is performed using unsigned integers and overflow. Most PRNGs use large numbers and overflow to work, that's their secret sauce.
Perl does not have a native unsigned type so I had to learn the guts of how Perl does math so I could emulate it. I ended up coming up with two different implementations, a native Perl implementation, and a version that uses Math::Int64
. Surprisingly the native version was significantly faster.
Both implementations with detailed comments are available here if you're interested in learning more about how to do integer style math in Perl.
I learned a lot, and it was a fun exercise. Thanks to reddit.com/u/DrHydeous for helping me with the integer rollover.
submitted by /u/scottchiefbaker
[link] [comments]