PRNG: PCG64 in Perl

I found a PCG64 implementation that I was able to borrow and convert to pure Perl:

# PCG64 RXS-M-XS "simple" variant
#my $seeds = [12, 34];
#my $rand  = pcg64_perl($seeds);
sub pcg64_perl {
    my $seeds = $_[0];
    my $ret   = (($seeds->[0] >> (($seeds->[0] >> 59) + 5)) ^ $seeds->[0]);

    use integer;
    $ret *= 12605985483714917081;
    $seeds->[0] = $seeds->[0] * 6364136223846793005 + $seeds->[1];
    no integer;

    $ret = ($ret >> 43) ^ $ret;

    return $ret;
}

Update: Bonus points because it's really fast:

*               Rate xosh256**  biski64     sfc64     PCG32 Splitmix64     PCG64
xosh256**  1589825/s        --     -26%      -48%      -52%       -58%      -65%
biski64    2145923/s       35%       --      -30%      -35%       -43%      -53%
sfc64      3058104/s       92%      43%        --       -8%       -19%      -33%
PCG32      3311258/s      108%      54%        8%        --       -13%      -28%
Splitmix64 3787879/s      138%      77%       24%       14%         --      -17%
PCG64      4587156/s      189%     114%       50%       39%        21%        --
Tags:
Leave A Reply
All content licensed under the Creative Commons License