Pseudo-random numbers/Xorshift star: Difference between revisions

m
→‎{{header|Raku}}: simplify, remove some superstitious parenthesis, only mask when necessary
(→‎{{header|Factor}}: restrict seed to positive integers)
m (→‎{{header|Raku}}: simplify, remove some superstitious parenthesis, only mask when necessary)
Line 376:
{{trans|Python}}
 
Raku does not have unsigned Integers at this time (Integers are arbitrary sized) so use explicit bit masks during bitwise operations. All constants are encapsulated inside the class.
 
<lang perl6>class Xorshift-star {
has $!seed;
has $!state;
 
constant mask64 = 2⁶⁴ - 1;
constant const = 0x2545F4914F6CDD1D;
 
submethod BUILD ( Int :$seed where * > 0 = 1 ) { #$!state default= $seed: 1+& mask64 }
$!seed = $seed;
$!state = $!seed +& mask64
}
 
method next-int {
$!state +^= ($!state +> 12) +& mask64;
$!state +^= ($!state +< 25) +& mask64;
$!state +^= ($!state +> 27) +& mask64;
(($!state * const) +> 32) +& (2³² - 1)
}
 
10,327

edits