Elementary cellular automaton/Random number generator: Difference between revisions

From Rosetta Code
Content added Content deleted
m (calling them pseudo-random at least once)
Line 9: Line 9:


For extra-credits, you will make this algorithm run as fast as possible in your language, for instance with an extensive use of bitwise logic.
For extra-credits, you will make this algorithm run as fast as possible in your language, for instance with an extensive use of bitwise logic.

=={{header|Perl}}==
{{trans|Perl 6}}

Since Perl 5 is currently much faster that Perl 6, we used a larger array size.

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

for my $n (1 .. 10) {
my $sum = 0;
for my $b (1 .. 8) {
$sum = $sum * 2 + $a->{cells}[0];
$a->next;
}
print $sum, $n == 10 ? "\n" : " ";
}</lang>
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>


=={{header|Perl 6}}==
=={{header|Perl 6}}==
A very easy to write, yet terribly slow version.
A very easy to write, yet terribly slow version.


<lang Perl 6>my Automaton $a .= new: :rule(30), :cells( 1, 0 xx 40 );
<lang perl6>my Automaton $a .= new: :rule(30), :cells( 1, 0 xx 40 );


say :2[$a++.cells[0] xx 8] xx 10;
say :2[$a++.cells[0] xx 8] xx 10;

Revision as of 21:26, 19 March 2014

Elementary cellular automaton/Random number generator is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Rule 30 is considered to be chaotic enough to generate good pseudo-random numbers. As a matter of fact, rule 30 is used by the Mathematica software for its default random number generator.

Steven Wolfram's recommendation for random number generation from rule 30 consists in extracting successive bits in a fixed position in the array of cells, as the automaton changes state.

The purpose of this task is to demonstrate this. With the code written in the parent task, which you don't need to re-write here, show the ten first bytes that emerge from this recommendation. To be precise, you will start with a state of all cells but one equal to zero, and you'll follow the evolution of the particular cell whose state was initially one. Then you'll regroup those bits by packets of eight, reconstituting bytes with a little-endian encoding.

You can pick which ever length you want for the initial array but it should be visible in the code so that your output can be reproduced with an other language.

For extra-credits, you will make this algorithm run as fast as possible in your language, for instance with an extensive use of bitwise logic.

Perl

Translation of: Perl 6

Since Perl 5 is currently much faster that Perl 6, we used a larger array size.

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

for my $n (1 .. 10) {

   my $sum = 0;
   for my $b (1 .. 8) {

$sum = $sum * 2 + $a->{cells}[0]; $a->next;

   }
   print $sum, $n == 10 ? "\n" : " ";

}</lang>

Output:
220 197 147 174 117 97 149 171 240 241

Perl 6

A very easy to write, yet terribly slow version.

<lang perl6>my Automaton $a .= new: :rule(30), :cells( 1, 0 xx 40 );

say :2[$a++.cells[0] xx 8] xx 10; </lang>

Output:
220 197 147 174 117 39 39 136 93 11