Elementary cellular automaton/Random number generator

From Rosetta Code
Revision as of 20:52, 19 March 2014 by Grondilu (talk | contribs) (Created page with "wp:Rule 30 is considered to be chaotic enough to generate good random numbers. As a matter of fact, rule 30 is used by the wp:Mathematica software for its default r...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Rule 30 is considered to be chaotic enough to generate good 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 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 0, and you'll follow the evolution of the cells whose state was initially one. Then you'll regroup those bits by packets of eight, reconstituting bytes with a little-endian encoding.

You will chose an array size large enough so that the way the automaton behaves at the boundaries will not matter. With ten bytes, thus eighty bits, a size of 80 / 2 = 40 cells should be enough since the automaton will not reach the boundaries in that time frame.

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 6

A very easy to write, yet terribly slow version.

<lang Perl 6>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