How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Perl programming language

Table of Contents

Problem Statement

Splitmix64 is the default pseudo-random number generator algorithm in Java and is included / available in many other languages. It uses a fairly simple algorithm that, though it is considered to be poor for cryptographic purposes, is very fast to calculate, and is "good enough" for many random number needs. It passes several fairly rigorous PRNG "fitness" tests that some more complex algorithms fail. Splitmix64 is not recommended for demanding random number requirements, but is often used to calculate initial states for other more complex pseudo-random number generators. The "standard" splitmix64 maintains one 64 bit state variable and returns 64 bits of random data with each call. Basic pseudocode algorithm: The returned value should hold 64 bits of numeric data. If your language does not support unsigned 64 bit integers directly you may need to apply appropriate bitmasks during bitwise operations. In keeping with the general layout of several recent pseudo-random number tasks:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Perl programming language

Source code in the perl programming language

use strict;
use warnings;
no warnings 'portable';
use feature 'say';
use Math::AnyNum qw(:overload);

package splitmix64 {

    sub new {
        my ($class, %opt) = @_;
        bless {state => $opt{seed}}, $class;
    }

    sub next_int {
        my ($self) = @_;
        my $next = $self->{state} = ($self->{state} + 0x9e3779b97f4a7c15) & (2**64 - 1);
        $next = ($next ^ ($next >> 30)) * 0xbf58476d1ce4e5b9 & (2**64 - 1);
        $next = ($next ^ ($next >> 27)) * 0x94d049bb133111eb & (2**64 - 1);
        ($next ^ ($next >> 31)) & (2**64 - 1);
    }

    sub next_float {
        my ($self) = @_;
        $self->next_int / 2**64;
    }
}

say 'Seed: 1234567, first 5 values:';
my $rng = splitmix64->new(seed => 1234567);
say $rng->next_int for 1 .. 5;

my %h;
say "\nSeed: 987654321, values histogram:";
$rng = splitmix64->new(seed => 987654321);
$h{int 5 * $rng->next_float}++ for 1 .. 100_000;
say "$_ $h{$_}" for sort keys %h;


  

You may also check:How to resolve the algorithm Date format step by step in the Joy programming language
You may also check:How to resolve the algorithm Plasma effect step by step in the AWK programming language
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the Tcl programming language
You may also check:How to resolve the algorithm Subleq step by step in the Wren programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the Bracmat programming language