Pseudo-random numbers/Xorshift star: Difference between revisions
Content added Content deleted
(→{{header|Factor}}: restrict seed to positive integers) |
Thundergnat (talk | contribs) m (→{{header|Raku}}: simplify, remove some superstitious parenthesis, only mask when necessary) |
||
Line 376: | Line 376: | ||
{{trans|Python}} |
{{trans|Python}} |
||
Raku does not have unsigned Integers at this time (Integers are arbitrary sized) so use explicit bit masks during bitwise operations. |
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 { |
<lang perl6>class Xorshift-star { |
||
has $!seed; |
|||
has $!state; |
has $!state; |
||
constant mask64 = 2⁶⁴ - 1; |
constant mask64 = 2⁶⁴ - 1; |
||
constant const = 0x2545F4914F6CDD1D; |
constant const = 0x2545F4914F6CDD1D; |
||
submethod BUILD ( Int :$seed where * > 0 = 1 ) { |
submethod BUILD ( Int :$seed where * > 0 = 1 ) { $!state = $seed +& mask64 } |
||
$!seed = $seed; |
|||
$!state = $!seed +& mask64 |
|||
} |
|||
method next-int { |
method next-int { |
||
$!state +^= |
$!state +^= $!state +> 12; |
||
$!state +^= |
$!state +^= $!state +< 25 +& mask64; |
||
$!state +^= |
$!state +^= $!state +> 27; |
||
($!state * const) +> 32 +& (2³² - 1) |
|||
} |
} |
||