Elementary cellular automaton/Random number generator: Difference between revisions

Content added Content deleted
m (→‎{{header|Racket}}: Improve code layout)
(→‎{{header|Tcl}}: Added zkl)
Line 350: Line 350:
220,197,147,174,241,126,135,130,143,234
220,197,147,174,241,126,135,130,143,234
Note that as the number of state bits is increased (the parameter to the constructor), the sequence tends to a limit of <math>220,</math> <math>197,</math> <math>147,</math> <math>174,</math> <math>117,</math> <math>97,</math> <math>149,</math> <math>171,</math> <math>240,</math> <math>241,</math> <math>\ldots</math> and that deviations from this are due to interactions between the state modification “wavefront” as the automaton wraps round.
Note that as the number of state bits is increased (the parameter to the constructor), the sequence tends to a limit of <math>220,</math> <math>197,</math> <math>147,</math> <math>174,</math> <math>117,</math> <math>97,</math> <math>149,</math> <math>171,</math> <math>240,</math> <math>241,</math> <math>\ldots</math> and that deviations from this are due to interactions between the state modification “wavefront” as the automaton wraps round.

=={{header|zkl}}==
No attempts at extra credit and not fast.
<lang zkl>fcn rule(n){ n=n.toString(2); "00000000"[n.len() - 8,*] + n }
fcn applyRule(rule,cells){
cells=String(cells[-1],cells,cells[0]); // wrap edges
(cells.len() - 2).pump(String,'wrap(n){ rule[7 - cells[n,3].toInt(2)] })
}
fcn rand30{
var r30=rule(30), cells="0"*63 + 1; // 64 bits (8 bytes), arbitrary
n:=0;
do(8){
n=n*2 + cells[-1]; // append bit 0
cells=applyRule(r30,cells); // next state
}
n
}</lang>
Note that "var" in a function is "static" in C, ie function local variables, initialized once.
<lang zkl>do(10){ rand30().print(","); }</lang>
{{out}}
<pre>220,197,147,174,117,97,149,171,100,151,</pre>