Elementary cellular automaton/Random number generator: Difference between revisions

Added FreeBASIC
(New post.)
(Added FreeBASIC)
 
(3 intermediate revisions by 2 users not shown)
Line 1:
{{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 181:
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#}}==
Line 810 ⟶ 848:
<syntaxhighlight lang="raku" line>class Automaton {
has $.rule;
has @.cells handles <AT-POS>;
has @.code = $!rule.fmt('%08b').flip.comb».Int;
Line 828 ⟶ 866:
my Automaton $a .= new: :rule(30), :cells( flat 1, 0 xx 100 );
 
say :2[$a++.cells[0] xx 8] xx 10;</syntaxhighlight>
{{out}}
<pre>220 197 147 174 117 97 149 171 240 241</pre>
Line 966 ⟶ 1,004:
{{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="ecmascriptwren">import "./big" for BigInt
 
var n = 64
2,122

edits