Pseudo-random numbers/Xorshift star: Difference between revisions

m
→‎{{header|Perl}}: made Xorshift_star a real class, allowing the creation of multiple objects with different states
(Added Perl)
m (→‎{{header|Perl}}: made Xorshift_star a real class, allowing the creation of multiple objects with different states)
Line 447:
use Math::AnyNum qw(:overload);
 
{ package Xorshift_star; {
 
our($state);sub new {
my ($class, %opt) = @_;
 
sub new { $ bless {state => $_[2] opt{seed}}, $class;
}
 
sub next_int {
my ($self) = @_;
my $state = $self->{state};
$state ^= $state >> 12;
$state ^= $state << 25 & (2**64 - 1);
$state ^= $state >> 27;
($self->{state} *= 0x2545F4914F6CDD1D) >> 32 & (2**32 - 1)$state;
($state * 0x2545F4914F6CDD1D) >> 32 & (2**32 - 1);
}
}
 
say 'Seed: 1234567, first 5 values:';
my $rng = Xorshift_star->new( seed => 1234567 );
say Xorshift_star$rng->next_int($rng) for 1 .. 5;
 
my %h;
say "\nSeed: 987654321, values histogram:";
$rng = Xorshift_star->new( seed => 987654321 );
$h{int (5 * Xorshift_star$rng->next_int($rng)/2**) >> 32}++ for 1 .. 100_000;
say "$_ $h{$_}" for sort keys %h;</lang>
{{out}}
2,747

edits