Pseudo-random numbers/Xorshift star: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: simplify, remove some superstitious parenthesis, only mask when necessary)
m (→‎{{header|Raku}}: further simplification and in-lining)
Line 381: Line 381:
has $!state;
has $!state;


submethod BUILD ( Int :$seed where * > 0 = 1 ) { $!state = $seed }
constant mask64 = 2⁶⁴ - 1;
constant const = 0x2545F4914F6CDD1D;

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


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


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



# Test next-int
# Test next-int