Elementary cellular automaton/Random number generator: Difference between revisions

Added FreeBASIC
m (→‎{{header|Perl}}: Fix link: Perl 6 --> Raku)
(Added FreeBASIC)
 
(16 intermediate revisions by 10 users not shown)
Line 1:
{{draft task}}
[[wp:Rule 30|Rule 30]] is considered to be chaotic enough to generate good pseudo-random numbers. As a matter of fact, for a long time rule 30 iswas used by the [[wp:Mathematica|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.
Line 12:
;Reference:
* [http://www.cs.indiana.edu/~dgerman/2005midwestNKSconference/dgelbm.pdf Cellular automata: Is Rule 30 random]? (PDF).
 
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V n = 64
 
F pow2(x)
R UInt64(1) << x
 
F evolve(UInt64 =state; rule)
L 10
V b = UInt64(0)
L(q) (7 .. 0).step(-1)
V st = state
b [|]= (st [&] 1) << q
state = 0
L(i) 0 .< :n
V t = ((st >> (i - 1)) [|] (st << (:n + 1 - i))) [&] 7
I (rule [&] pow2(t)) != 0
state [|]= pow2(i)
print(‘ ’b, end' ‘’)
print()
 
evolve(1, 30)</syntaxhighlight>
 
{{out}}
<pre>
220 197 147 174 117 97 149 171 100 151
</pre>
 
=={{header|C}}==
64-bits array size, cyclic borders.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <limits.h>
 
Line 45 ⟶ 75:
evolve(1, 30);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 220 197 147 174 117 97 149 171 100 151</pre>
Line 51 ⟶ 81:
=={{header|C++}}==
We'll re-write the code of the parent task here.
<langsyntaxhighlight lang="cpp">#include <bitset>
#include <stdio.h>
 
Line 88 ⟶ 118:
printf("%u%c", byte(state), i ? ' ' : '\n');
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>
Line 95 ⟶ 125:
{{trans|C}}
Adapted from the C version, with improvements and bug fixes. Optimized for performance as requested in the task description. This is a lazy range.
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.typecons;
 
struct CellularRNG {
Line 146 ⟶ 176:
CellularRNG(1, 30).take(10).writeln;
CellularRNG(1, 30).drop(2_000_000).front.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[220, 197, 147, 174, 117, 97, 149, 171, 100, 151]
44</pre>
Run-time: less than two seconds with the ldc2 compiler.
 
=={{header|FreeBASIC}}==
{{trans|Go}}
<syntaxhighlight lang="vbnet">Const n As Uinteger = 64
 
#define pow2(x) Culng(1) Shl x
 
Sub Evolve(state As Integer, rule As Integer)
Dim As Integer i, p, q
Dim As Ulongint b, st, t1, t2, t3
For p = 0 To 9
b = 0
For q = 7 To 0 Step -1
st = state
b Or= (st And 1) Shl q
state = 0
For i = 0 To n - 1
t1 = Iif(i > 0, st Shr (i - 1), st Shr 63)
Select Case i
Case 0: t2 = st Shl 1
Case 1: t2 = st Shl 63
Case Else: t2 = st Shl (n + 1 - i)
End Select
t3 = 7 And (t1 Or t2)
If (rule And pow2(t3)) <> 0 Then state Or= pow2(i)
Next i
Next q
Print Using "####"; b;
Next p
Print
End Sub
 
Evolve(1, 30)
 
Sleep</syntaxhighlight>
{{out}}
<pre> 220 197 147 174 117 97 149 171 100 151</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [[Elementary cellular automaton#The_Function]]
<langsyntaxhighlight lang="fsharp">
// Generate random numbers using Rule 30. Nigel Galloway: August 1st., 2019
eca 30 [|yield 1; yield! Array.zeroCreate 99|]|>Seq.chunkBySize 8|>Seq.map(fun n->n|>Array.mapi(fun n g->g.[0]<<<(7-n))|>Array.sum)|>Seq.take 10|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 165 ⟶ 233:
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 210 ⟶ 278:
func main() {
evolve(1, 30)
}</langsyntaxhighlight>
 
{{out}}
Line 221 ⟶ 289:
Assume the comonadic solution given at [[Elementary cellular automaton#Haskell]] is packed in a module <code>CellularAutomata</code>
 
<langsyntaxhighlight Haskelllang="haskell">import CellularAutomata (runCAfromList, rule, fromListrunCA)
import Data.List (unfoldr)
import Control.Comonad
import Data.List (unfoldr)
 
rnd = fromBits <$> unfoldr (pure . splitAt 8) bits
where size = 80
size = 80
bits = extract <$> runCA (rule 30) (fromList (1:replicate size 0))
bits =
extract
<$> runCA
(rule 30)
(fromList (1 : replicate size 0))
 
fromBits = foldl (\res(+) x ->. (2*res + x*)) 0</langsyntaxhighlight>
 
{{Out}}
Line 237 ⟶ 310:
Using the rule 30 CA it is possible to determine the <code>RandomGen</code> instance which could be utilized by the <code>Random</code> class:
 
<langsyntaxhighlight Haskelllang="haskell">import System.Random
 
instance RandomGen (Cycle Int) where
next c =
next c = let x = c =>> step (rule 30) in (fromBits (view x), x)
split c let x = (c, fromList=>> (reversestep (viewrule c30)))</lang>
in (fromBits (view x), x)
split = (,) <*> (fromList . reverse . view)</syntaxhighlight>
 
<pre>λ> let r30 = fromList [1,0,1,0,1,0,1,0,1,0,1,0,1] :: Cycle Int
Line 263 ⟶ 338:
=={{header|J}}==
ca is a cellular automata class. The rng class inherits ca and extends it with bit and byte verbs to sample the ca.
<syntaxhighlight lang="j">
<lang J>
coclass'ca'
DOC =: 'locale creation: (RULE ; INITIAL_STATE) conew ''ca'''
Line 275 ⟶ 350:
byte =: [: #. [: , [: bit"0 (i.8)"_
coclass'base'
</syntaxhighlight>
</lang>
Having installed these into a j session we create and use the mathematica prng.
<pre>
Line 281 ⟶ 356:
byte__m"0 i.10
220 197 147 174 117 97 149 171 100 151
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public class ElementaryCellularAutomatonRandomNumberGenerator {
 
public static void main(String[] aArgs) {
final int seed = 989898989;
evolve(seed, 30);
}
private static void evolve(int aState, int aRule) {
long state = aState;
for ( int i = 0; i <= 9; i++ ) {
int b = 0;
for ( int q = 7; q >= 0; q-- ) {
long stateCopy = state;
b |= ( stateCopy & 1 ) << q;
state = 0;
for ( int j = 0; j < BIT_COUNT; j++ ) {
long t = ( stateCopy >>> ( j - 1 ) ) | ( stateCopy << ( BIT_COUNT + 1 - j ) ) & 7;
if ( ( aRule & ( 1L << t ) ) != 0 ) {
state |= 1 << j;
}
}
}
System.out.print(" " + b);
}
System.out.println();
}
private static final int BIT_COUNT = 64;
 
}
</syntaxhighlight>
{{ out }}
<pre>
231 223 191 126 253 251 247 239 223 191
</pre>
 
=={{header|jq}}==
'''Works with jq and gojq, the C and Go implementations of jq'''
 
The following also works with jaq, the Rust implementation of jq, provided
the "include" directive is replaced with the set of definitions from
the parent task, and that a suitable alternative to 100*"0" is
presented.
 
<syntaxhighlight lang=jq>
include "elementary-cellular-automaton" {search : "."};
 
# If using jq, the def of _nwise can be omitted.
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
# Input: an array of bits represented by 0s, 1s, "0"s, or "1"s
# Output: the corresponding decimal on the assumption that the leading bits are least significant,
# e.g. [0,1] => 2
def binary2number:
reduce (.[]|tonumber) as $x ({p:1}; .n += .p * $x | .p *= 2) | .n;
("1" + 100 * "0" ) | [automaton(30; 80) | .[0:1]] | [_nwise(8) | reverse | binary2number]
</syntaxhighlight>
{{output}}
<pre>
[220,197,147,174,117,97,149,171,240,241]
</pre>
 
=={{header|Julia}}==
{{trans|C, Go}}
<langsyntaxhighlight lang="julia">function evolve(state, rule, N=64)
B(x) = UInt64(1) << x
for p in 0:9
Line 307 ⟶ 449:
 
evolve(1, 30)
</langsyntaxhighlight>{{out}}
<pre>
220 197 147 174 117 97 149 171 100 151
Line 314 ⟶ 456:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
const val N = 64
Line 340 ⟶ 482:
fun main(args: Array<String>) {
evolve(1, 30)
}</langsyntaxhighlight>
 
{{out}}
Line 346 ⟶ 488:
220 197 147 174 117 97 149 171 100 151
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">FromDigits[#, 2] & /@ Partition[Flatten[CellularAutomaton[30, {{1}, 0}, {200, 0}]], 8]</syntaxhighlight>
{{out}}
<pre>{220, 197, 147, 174, 117, 97, 149, 171, 240, 241, 92, 18, 199, 27, 104, 8, 251, 167, 29, 112, 100, 103, 159, 129, 253}</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">const N = 64
 
template pow2(x: uint): uint = 1u shl x
 
proc evolve(state: uint; rule: Positive) =
var state = state
for _ in 1..10:
var b = 0u
for q in countdown(7, 0):
let st = state
b = b or (st and 1) shl q
state = 0
for i in 0u..<N:
let t = (st shr (i - 1) or st shl (N + 1 - i)) and 7
if (rule.uint and pow2(t)) != 0: state = state or pow2(i)
stdout.write ' ', b
echo ""
 
evolve(1, 30)</syntaxhighlight>
 
{{out}}
<pre> 220 197 147 174 117 97 149 171 100 151</pre>
 
=={{header|Pascal}}==
{{Works with|Free Pascal}}
Using ROR and ROL is as fast as assembler and more portable.<BR>[https://tio.run/##7VZdb@pGEH33r5iHSEAvYJsQ0kBTifBxawmwC6a9bVVFjr3AKmZtrZdwaZS/Xjq7iwPckOThvvQhSHx45szMmbPD7qZBFgZxZZaG263HkzkPljBexeTcahmmuRAibZomYdU1vacpiWhQTfjclE/miHwVtxMRCHIrI26PQpaBWKwTHkfVdRLPMGs1TJamzlxdiGVsPJ45/W6vD32v82QAPJ4Nk4hAl8Tpgj49nrUnw6Hb7YEz8nsDDXA93xk6f7Z9xx2BOyq3B@gwTenqILQ9cD6PIOVJeG0/YfreYNLTgW3P8//wetBxRxN30FPOUdfpPxmrjGQImWyyqaBx1jLChGUCLcvg6zhZsSiDa6j9YFuWpT5a6OLajq9rsNFiPAQczbo3LQg0YUqZaNTRO1uxUNCEwWciOt701qdL0oSdV2xSgrF@J11hNk7ChEcGHLx@oegqH5kGiUQ3oYv6Rq29izB80lwQIBAh07aMOzKnDI1BtpQ0u/6kI6OG7m86BXiCw18I9asq9d/lXvvLKwBFBwFdCVAFAdZULHTFKFFsOMlWMda/1l0WMcibliBbxHBeg0@6gZahwg25XiRacQIOo@JQxBZlMWVk38ChE5PbL1OcGMk8iRaGr1gZR4Q8lBlC96uUl0A/SOJHPNDuYYiyu@NfpSRFmcguSZdMpF2Db11HIyExRQV2x7JOCb7gD8kl7@N5UmQbOvZmg62OAkEfiMOE/H816pUbx4cwYIDDi3PKNij4nGaC8OydLnGsNy5T0@loy807fe@X0tqHgxYfwDSnjCcxCjyHgEWgtJYPL9cB7jawQMxRzqL@@Ul/laQwUj1oj7q61HsCv7EseUevSf5B5IPIB5H/LZHj0/S9nXN/AkxSQiJBMpHv@L5d9i3c8ZzRbgukTTjYUPMNcM2pIDErFp4TwCzhkMn6Gf2HQDKDQrlRLxfgjoqsUNL73@8BX8IqlTguQCSwDu6JNOCJh4A@pqBqu9zdH9RHxcaTcs0QbeVH5qm7lCRvyeCDi4Os@uKc3BXSlZ4vLq9U2Z8rLaXOiQK5Fsfw0qGrEG7CmGSQEg7SiQIXykXfrgirZD5TaFrN2mHYy@Xyg@w@XymqbkVkTviJZfEXBASCQSv/tga2XIndNW3Xukr0TUfN@ilyeWk1CL6aJNjxzNMU4KceXkvHP0s2nATRLrqK5zNec1MakwjkQU2F8cY8Nepqlox63XgpJ16Try4MI@/bgFrNAvvqEuw6vi/rYNuXAOr5Cp9tOWJgX9hGzs04JHNe@y4ydu3H6kXju9hst/@GsziYZ9uKe76tTB7@Aw Try it online!] counting CPU-Cycles 32 vs 31 on Ryzen Zen1 per Byte -> 100Mb/s
<langsyntaxhighlight lang="pascal">Program Rule30;
//http://en.wikipedia.org/wiki/Next_State_Rule_30;
//http://mathworld.wolfram.com/Rule30.html
Line 489 ⟶ 661:
Task;
write(' <ENTER> ');readln;
end.</langsyntaxhighlight>
{{out}}
<pre>//compiled 64-Bit
Line 511 ⟶ 683:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">package Automaton {
sub new {
my $class = shift;
Line 547 ⟶ 719:
}
print $sum, $n == 10 ? "\n" : " ";
}</langsyntaxhighlight>
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>
Line 555 ⟶ 727:
and with the changes marked [2] C++, Haskell, Perl, Python, Ruby, Scheme, and Sidef, but completely different to Rust and Tcl.
No attempt to optimise.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>--string s = ".........#.........", --(original)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
string s = "...............................#"&
<span style="color: #000080;font-style:italic;">--string s = ".......................#.........", --(original)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"...............................#"</span><span style="color: #0000FF;">&</span>
--string s = "#"&repeat('.',100), -- [2]
<span style="color: #008000;">"................................"</span><span style="color: #0000FF;">,</span>
t=s, r = "........"
<span style="color: #000080;font-style:italic;">--string s = "#"&repeat('.',100), -- [2]</span>
integer rule = 30, k, l = length(s), w = 0
<span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"........"</span>
for i=1 to 8 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">rule</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">30</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
r[i] = iff(mod(rule,2)?'#':'.')
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
rule = floor(rule/2)
<span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">:</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">rule</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
sequence res = {}
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=0 to 80 do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
w = w*2 + (s[32]='#')
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">80</span> <span style="color: #008080;">do</span>
-- w = w*2 + (s[1]='#') -- [2]
<span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">32</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">)</span>
if mod(i+1,8)=0 then res&=w w=0 end if
<span style="color: #000080;font-style:italic;">-- w = w*2 + (s[1]='#') -- [2]</span>
for j=1 to l do
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">&=</span><span style="color: #000000;">w</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
k = (s[iff(j=1?l:j-1)]='#')*4
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
+ (s[ j ]='#')*2
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">l</span><span style="color: #0000FF;">:</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)]=</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">4</span>
+ (s[iff(j=l?1:j+1)]='#')+1
<span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">]=</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span>
t[j] = r[k]
<span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)]=</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
end for
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
s = t
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span>
?res</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 591 ⟶ 766:
=={{header|Python}}==
===Python: With zero padded ends===
<langsyntaxhighlight lang="python">from elementary_cellular_automaton import eca, eca_wrap
 
def rule30bytes(lencells=100):
Line 600 ⟶ 775:
 
if __name__ == '__main__':
print([b for i,b in zip(range(10), rule30bytes())])</langsyntaxhighlight>
 
{{out}}
Line 607 ⟶ 782:
 
===Python: With wrapping of end cells===
<langsyntaxhighlight lang="python">def rule30bytes(lencells=100):
cells = '1' + '0' * (lencells - 1)
gen = eca_wrap(cells, 30)
while True:
yield int(''.join(next(gen)[0] for i in range(8)), 2))</langsyntaxhighlight>
 
{{out}}
Line 620 ⟶ 795:
Implementation of [[Elementary cellular automaton]] is saved in "Elementary_cellular_automata.rkt"
 
<langsyntaxhighlight lang="racket">#lang racket
;; below is the code from the parent task
(require "Elementary_cellular_automata.rkt")
Line 659 ⟶ 834:
(number->string (C30-rand-64 256) 16)
(number->string (C30-rand-64 256) 16)
(number->string (C30-rand-64 256) 16))</langsyntaxhighlight>
 
{{out}}
Line 671 ⟶ 846:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>class Automaton {
has $.rule;
has @.cells handles <AT-POS>;
has @.code = $!rule.fmt('%08b').flip.comb».Int;
Line 691 ⟶ 866:
my Automaton $a .= new: :rule(30), :cells( flat 1, 0 xx 100 );
 
say :2[$a++.cells[0] xx 8] xx 10;</langsyntaxhighlight>
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">size = 100
eca = ElemCellAutomat.new("1"+"0"*(size-1), 30)
eca.take(80).map{|line| line[0]}.each_slice(8){|bin| p bin.join.to_i(2)}</langsyntaxhighlight>
{{out}}
<pre>
Line 714 ⟶ 889:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
//Assuming the code from the Elementary cellular automaton task is in the namespace.
fn main() {
Line 736 ⟶ 911:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 743 ⟶ 918:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">
; uses SRFI-1 library http://srfi.schemers.org/srfi-1/srfi-1.html
 
Line 763 ⟶ 938:
 
(random-r30 10)
</syntaxhighlight>
</lang>
 
{{out}}
Line 772 ⟶ 947:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var auto = Automaton(30, [1] + 100.of(0));
 
10.times {
Line 781 ⟶ 956:
};
say sum;
};</langsyntaxhighlight>
{{out}}
<pre>
Line 798 ⟶ 973:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">oo::class create RandomGenerator {
superclass ElementaryAutomaton
variable s
Line 814 ⟶ 989:
return [scan [join $bits ""] %b]
}
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">set rng [RandomGenerator new 31]
for {set r {}} {[llength $r]<10} {} {
lappend r [$rng rand]
}
puts [join $r ,]</langsyntaxhighlight>
{{out}}
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.
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-big}}
As Wren cannot deal accurately with 64-bit unsigned integers and bit-wise operations thereon, we need to use BigInt here.
<syntaxhighlight lang="wren">import "./big" for BigInt
 
var n = 64
 
var pow2 = Fn.new { |x| BigInt.one << x }
 
var evolve = Fn.new { |state, rule|
for (p in 0..9) {
var b = BigInt.zero
for (q in 7..0) {
var st = state.copy()
b = b | ((st & 1) << q)
state = BigInt.zero
for (i in 0...n) {
var t1 = (i > 0) ? st >> (i-1) : st >> 63
var t2 = (i == 0) ? st << 1 : (i == 1) ? st << 63 : st << (n+1-i)
var t3 = (t1 | t2) & 7
if ((pow2.call(t3) & rule) != BigInt.zero) state = state | pow2.call(i)
}
}
System.write(" %(b)")
}
System.print()
}
 
evolve.call(BigInt.one, 30)</syntaxhighlight>
 
{{out}}
<pre>
220 197 147 174 117 97 149 171 100 151
</pre>
 
=={{header|zkl}}==
No attempts at extra credit and not fast.
<langsyntaxhighlight 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
Line 840 ⟶ 1,051:
}
n
}</langsyntaxhighlight>
Note that "var" in a function is "static" in C, ie function local variables, initialized once.
<langsyntaxhighlight lang="zkl">do(10){ rand30().print(","); }</langsyntaxhighlight>
{{out}}
<pre>220,197,147,174,117,97,149,171,100,151,</pre>
2,122

edits