How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Raku programming language

Table of Contents

Problem Statement

numbers as shown above. are as shown above

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Raku programming language

Source code in the raku programming language

class Xorshift-star {
    has $!state;

    submethod BUILD ( Int :$seed where * > 0 = 1 ) { $!state = $seed }

    method next-int {
        $!state +^= $!state +> 12;
        $!state +^= $!state +< 25 +& (2⁶⁴ - 1);
        $!state +^= $!state +> 27;
        ($!state * 0x2545F4914F6CDD1D) +> 32 +& (2³² - 1)
    }

    method next-rat { self.next-int / 2³² }
}

# Test next-int
say 'Seed: 1234567; first five Int values';
my $rng = Xorshift-star.new( :seed(1234567) );
.say for $rng.next-int xx 5;


# Test next-rat (since these are rational numbers by default)
say "\nSeed: 987654321; first 1e5 Rat values histogram";
$rng = Xorshift-star.new( :seed(987654321) );
say ( ($rng.next-rat * 5).floor xx 100_000 ).Bag;


# Test with default seed
say "\nSeed: default; first five Int values";
$rng = Xorshift-star.new;
.say for $rng.next-int xx 5;


  

You may also check:How to resolve the algorithm Gaussian elimination step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the bc programming language
You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the J programming language
You may also check:How to resolve the algorithm Stack step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the 6502 Assembly programming language