Elementary cellular automaton/Random number generator: Difference between revisions
Elementary cellular automaton/Random number generator (view source)
Revision as of 10:12, 27 August 2022
, 1 year agosntax highlighting fixup automation
Thundergnat (talk | contribs) m (Thundergnat moved page Elementary cellular automaton/Random Number Generator to Elementary cellular automaton/Random number generator: capitalization policy) |
Thundergnat (talk | contribs) m (sntax highlighting fixup automation) |
||
Line 17:
{{trans|Nim}}
<
F pow2(x)
Line 36:
print()
evolve(1, 30)</
{{out}}
Line 45:
=={{header|C}}==
64-bits array size, cyclic borders.
<
#include <limits.h>
Line 75:
evolve(1, 30);
return 0;
}</
{{out}}
<pre> 220 197 147 174 117 97 149 171 100 151</pre>
Line 81:
=={{header|C++}}==
We'll re-write the code of the parent task here.
<
#include <stdio.h>
Line 118:
printf("%u%c", byte(state), i ? ' ' : '\n');
return 0;
}</
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>
Line 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.
<
struct CellularRNG {
Line 176:
CellularRNG(1, 30).take(10).writeln;
CellularRNG(1, 30).drop(2_000_000).front.writeln;
}</
{{out}}
<pre>[220, 197, 147, 174, 117, 97, 149, 171, 100, 151]
Line 184:
=={{header|F_Sharp|F#}}==
This task uses [[Elementary cellular automaton#The_Function]]
<
// 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>
{{out}}
<pre>
Line 195:
=={{header|Go}}==
{{trans|C}}
<
import "fmt"
Line 240:
func main() {
evolve(1, 30)
}</
{{out}}
Line 251:
Assume the comonadic solution given at [[Elementary cellular automaton#Haskell]] is packed in a module <code>CellularAutomata</code>
<
import Control.Comonad
import Data.List (unfoldr)
Line 264:
(fromList (1 : replicate size 0))
fromBits = foldl ((+) . (2 *)) 0</
{{Out}}
Line 272:
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:
<
instance RandomGen (Cycle Int) where
Line 278:
let x = c =>> step (rule 30)
in (fromBits (view x), x)
split = (,) <*> (fromList . reverse . view)</
<pre>λ> let r30 = fromList [1,0,1,0,1,0,1,0,1,0,1,0,1] :: Cycle Int
Line 300:
=={{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">
coclass'ca'
DOC =: 'locale creation: (RULE ; INITIAL_STATE) conew ''ca'''
Line 312:
byte =: [: #. [: , [: bit"0 (i.8)"_
coclass'base'
</syntaxhighlight>
Having installed these into a j session we create and use the mathematica prng.
<pre>
Line 322:
=={{header|Julia}}==
{{trans|C, Go}}
<
B(x) = UInt64(1) << x
for p in 0:9
Line 344:
evolve(1, 30)
</
<pre>
220 197 147 174 117 97 149 171 100 151
Line 351:
=={{header|Kotlin}}==
{{trans|C}}
<
const val N = 64
Line 377:
fun main(args: Array<String>) {
evolve(1, 30)
}</
{{out}}
Line 385:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
{{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>
Line 391:
=={{header|Nim}}==
{{trans|Kotlin}}
<
template pow2(x: uint): uint = 1u shl x
Line 409:
echo ""
evolve(1, 30)</
{{out}}
Line 417:
{{Works with|Free Pascal}}
Using ROR and ROL is as fast as assembler and more portable.<BR>[https://tio.run/##7VZdb@pGEH33r5iHSEAvYJsQ0kBTifBxawmwC6a9bVVFjr3AKmZtrZdwaZS/Xjq7iwPckOThvvQhSHx45szMmbPD7qZBFgZxZZaG263HkzkPljBexeTcahmmuRAibZomYdU1vacpiWhQTfjclE/miHwVtxMRCHIrI26PQpaBWKwTHkfVdRLPMGs1TJamzlxdiGVsPJ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e@y4ydu3H6kXju9hst/@GsziYZ9uKe76tTB7@Aw Try it online!] counting CPU-Cycles 32 vs 31 on Ryzen Zen1 per Byte -> 100Mb/s
<
//http://en.wikipedia.org/wiki/Next_State_Rule_30;
//http://mathworld.wolfram.com/Rule30.html
Line 556:
Task;
write(' <ENTER> ');readln;
end.</
{{out}}
<pre>//compiled 64-Bit
Line 578:
=={{header|Perl}}==
{{trans|Raku}}
<
sub new {
my $class = shift;
Line 614:
}
print $sum, $n == 10 ? "\n" : " ";
}</
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>
Line 622:
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.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #000080;font-style:italic;">--string s = ".........#.........", --(original)</span>
Line 648:
<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>
<!--</
{{out}}
<pre>
Line 661:
=={{header|Python}}==
===Python: With zero padded ends===
<
def rule30bytes(lencells=100):
Line 670:
if __name__ == '__main__':
print([b for i,b in zip(range(10), rule30bytes())])</
{{out}}
Line 677:
===Python: With wrapping of end cells===
<
cells = '1' + '0' * (lencells - 1)
gen = eca_wrap(cells, 30)
while True:
yield int(''.join(next(gen)[0] for i in range(8)), 2))</
{{out}}
Line 690:
Implementation of [[Elementary cellular automaton]] is saved in "Elementary_cellular_automata.rkt"
<
;; below is the code from the parent task
(require "Elementary_cellular_automata.rkt")
Line 729:
(number->string (C30-rand-64 256) 16)
(number->string (C30-rand-64 256) 16)
(number->string (C30-rand-64 256) 16))</
{{out}}
Line 741:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
has $.rule;
has @.cells;
Line 761:
my Automaton $a .= new: :rule(30), :cells( flat 1, 0 xx 100 );
say :2[$a++.cells[0] xx 8] xx 10;</
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>
=={{header|Ruby}}==
<
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)}</
{{out}}
<pre>
Line 784:
=={{header|Rust}}==
<
//Assuming the code from the Elementary cellular automaton task is in the namespace.
fn main() {
Line 806:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 813:
=={{header|Scheme}}==
<
; uses SRFI-1 library http://srfi.schemers.org/srfi-1/srfi-1.html
Line 833:
(random-r30 10)
</syntaxhighlight>
{{out}}
Line 842:
=={{header|Sidef}}==
<
10.times {
Line 851:
};
say sum;
};</
{{out}}
<pre>
Line 868:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
superclass ElementaryAutomaton
variable s
Line 884:
return [scan [join $bits ""] %b]
}
}</
Demonstrating:
<
for {set r {}} {[llength $r]<10} {} {
lappend r [$rng rand]
}
puts [join $r ,]</
{{out}}
220,197,147,174,241,126,135,130,143,234
Line 899:
{{libheader|Wren-big}}
As Wren cannot deal accurately with 64-bit unsigned integers and bit-wise operations thereon, we need to use BigInt here.
<
var n = 64
Line 924:
}
evolve.call(BigInt.one, 30)</
{{out}}
Line 933:
=={{header|zkl}}==
No attempts at extra credit and not fast.
<
fcn applyRule(rule,cells){
cells=String(cells[-1],cells,cells[0]); // wrap edges
Line 946:
}
n
}</
Note that "var" in a function is "static" in C, ie function local variables, initialized once.
<
{{out}}
<pre>220,197,147,174,117,97,149,171,100,151,</pre>
|