Elementary cellular automaton/Random number generator: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: Include referenced class to make runnable file)
m (→‎{{header|Perl}}: inline package code from 'Elementary cellular automaton' task)
Line 254: Line 254:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Perl 6}}
{{trans|Perl 6}}
<lang perl>my $a = Automaton->new(30, 1, map 0, 1 .. 100);
<lang perl>package Automaton {
sub new {
my $class = shift;
my $rule = [ reverse split //, sprintf "%08b", shift ];
return bless { rule => $rule, cells => [ @_ ] }, $class;
}
sub next {
my $this = shift;
my @previous = @{$this->{cells}};
$this->{cells} = [
@{$this->{rule}}[
map {
4*$previous[($_ - 1) % @previous]
+ 2*$previous[$_]
+ $previous[($_ + 1) % @previous]
} 0 .. @previous - 1
]
];
return $this;
}
use overload
q{""} => sub {
my $this = shift;
join '', map { $_ ? '#' : ' ' } @{$this->{cells}}
};
}

my $a = Automaton->new(30, 1, map 0, 1 .. 100);


for my $n (1 .. 10) {
for my $n (1 .. 10) {