Jump to content

Percolation/Mean run density: Difference between revisions

Added Julia language
m (→‎{{header|Pascal}}: Fixed typo in language tag)
(Added Julia language)
Line 594:
500 0.90 16384 0.090 0.090 0.1
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Python}}
<lang julia>using Distributions, IterTools
 
newv(n::Int, p::Float64) = rand(Bernoulli(p), n)
runs(v::Vector{Int}) = sum((a & ~b) for (a, b) in zip(v, IterTools.chain(v[2:end], v[1])))
 
mrd(n::Int, p::Float64) = runs(newv(n, p)) / n
 
nrep = 500
 
for p in 0.1:0.2:1
lim = p * (1 - p)
 
println()
for ex in 10:2:14
n = 2 ^ ex
sim = mean(mrd.(n, p) for _ in 1:nrep)
@printf("nrep = %3i\tp = %4.2f\tn = %5i\np · (1 - p) = %5.3f\tsim = %5.3f\tΔ = %3.1f%%\n",
nrep, p, n, lim, sim, lim > 0 ? abs(sim - lim) / lim * 100 : sim * 100)
end
end</lang>
 
{{out}}
<pre>
nrep = 500 p = 0.10 n = 1024
p · (1 - p) = 0.090 sim = 0.090 Δ = 0.4%
nrep = 500 p = 0.10 n = 4096
p · (1 - p) = 0.090 sim = 0.090 Δ = 0.2%
nrep = 500 p = 0.10 n = 16384
p · (1 - p) = 0.090 sim = 0.090 Δ = 0.0%
 
nrep = 500 p = 0.30 n = 1024
p · (1 - p) = 0.210 sim = 0.211 Δ = 0.5%
nrep = 500 p = 0.30 n = 4096
p · (1 - p) = 0.210 sim = 0.210 Δ = 0.1%
nrep = 500 p = 0.30 n = 16384
p · (1 - p) = 0.210 sim = 0.210 Δ = 0.0%
 
nrep = 500 p = 0.50 n = 1024
p · (1 - p) = 0.250 sim = 0.250 Δ = 0.0%
nrep = 500 p = 0.50 n = 4096
p · (1 - p) = 0.250 sim = 0.250 Δ = 0.1%
nrep = 500 p = 0.50 n = 16384
p · (1 - p) = 0.250 sim = 0.250 Δ = 0.0%
 
nrep = 500 p = 0.70 n = 1024
p · (1 - p) = 0.210 sim = 0.209 Δ = 0.3%
nrep = 500 p = 0.70 n = 4096
p · (1 - p) = 0.210 sim = 0.210 Δ = 0.1%
nrep = 500 p = 0.70 n = 16384
p · (1 - p) = 0.210 sim = 0.210 Δ = 0.0%
 
nrep = 500 p = 0.90 n = 1024
p · (1 - p) = 0.090 sim = 0.090 Δ = 0.0%
nrep = 500 p = 0.90 n = 4096
p · (1 - p) = 0.090 sim = 0.090 Δ = 0.0%
nrep = 500 p = 0.90 n = 16384
p · (1 - p) = 0.090 sim = 0.090 Δ = 0.1%</pre>
 
=={{header|Mathematica}}==
Line 637 ⟶ 698:
0.9 100000 0.0900 -0.0000144
</pre>
 
=={{header|Pascal}}==
{{trans|C}}{{works with|Free Pascal}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.