Elementary cellular automaton/Random number generator: Difference between revisions
Elementary cellular automaton/Random number generator (view source)
Revision as of 14:11, 31 July 2018
, 5 years ago→{{header|Perl}}: inline package code from 'Elementary cellular automaton' task
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Include referenced class to make runnable file) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: inline package code from 'Elementary cellular automaton' task) |
||
Line 254:
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>
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) {
|