Percolation/Mean run density: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 31:
{{trans|Python}}
<
F nonrandom()
:seed = 1664525 * :seed + 1013904223
Line 55:
V sim = sum((0 .< t).map(i -> mean_run_density(@n, :p))) / t
print(‘t=#3 p=#.2 n=#5 p(1-p)=#.3 sim=#.3 delta=#.1%’.format(
t, p, n, limit, sim, I limit {abs(sim - limit) / limit * 100} E sim * 100))</
{{out}}
Line 82:
=={{header|C}}==
<
#include <stdlib.h>
Line 117:
return 0;
}</
{{out}}
<pre>
Line 150:
=={{header|C++}}==
<
#include <random>
#include <vector>
Line 205:
}
return 0 ;
}</
{{out}}
<pre>t = 100
Line 237:
=={{header|D}}==
{{trans|python}}
<
enum n = 100, p = 0.5, t = 500;
Line 259:
}
}
}</
{{out}}
<pre>t=500, p=0.10, n= 1024, p(1-p)=0.09000, sim=0.08949, delta=0.6%
Line 282:
=={{header|EchoLisp}}==
<
;; count 1-runs - The vector is not stored
(define (runs p n)
Line 305:
(for ([n '(10 100 1000)])
(printf "\t-- n %5d → %d" n (truns p n)))))
</syntaxhighlight>
{{out}}
<pre>
Line 332:
=={{header|Factor}}==
<
random sequences ;
IN: rosetta-code.mean-run-density
Line 363:
: main ( -- ) header .1 .9 .2 <range> [ test ] each ;
MAIN: main</
{{out}}
<pre>
Line 390:
=={{header|Fortran}}==
<
! loosely translated from python. We do not need to generate and store the entire vector at once.
! compilation: gfortran -Wall -std=f2008 -o thisfile thisfile.f08
Line 450:
end program percolation_mean_run_density
</syntaxhighlight>
<pre>
Line 479:
=={{header|FreeBASIC}}==
{{trans|Phix}}
<
Dim As Integer r, l, cont = 0
Dim As Integer v, pv
Line 513:
Print
Next ip
Sleep</
<pre>Same as Phix, C, Kotlin, Wren, Pascal or zkl entry.</pre>
=={{header|Go}}==
<
import (
Line 551:
}
}
}</
{{out}}
<pre>
Line 591:
=={{header|Haskell}}==
<
import Control.Applicative
import Text.Printf
Line 619:
x <- newStdGen
forM_ [0.1,0.3,0.5,0.7,0.9] $ printKs x
</syntaxhighlight>
<pre>./percolation
p= 0.1, K(p)= 0.090
Line 657:
The following works in both languages:
<
t := integer(A[2]) | 500
Line 671:
write(left(p,8)," ",left(n,8)," ",left(p*(1-p),10)," ",left(Ka/t, 10))
}
end</
Output:
Line 697:
=={{header|J}}==
<syntaxhighlight lang="j">
NB. translation of python
Line 719:
EMPTY
)
</syntaxhighlight>
Session:
<pre>
Line 748:
=={{header|Julia}}==
{{trans|Python}}
<
newv(n::Int, p::Float64) = rand(Bernoulli(p), n)
Line 767:
nrep, p, n, lim, sim, lim > 0 ? abs(sim - lim) / lim * 100 : sim * 100)
end
end</
{{out}}
Line 808:
=={{header|Kotlin}}==
{{trans|C}}
<
import java.util.Random
Line 847:
println()
}
}</
Sample output:
Line 881:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Mean[Length[Cases[Split@#, {1, ___}]] & /@
Unitize[Chop[RandomReal[1, {trials, len}], 1 - p]]]/len
Line 889:
Table[{q, n, x = meanRunDensity[q, n, 100] // N,
q (1 - q) - x}, {n, {100, 1000, 10000, 100000}}], {}],
Alignment -> Left], {q, {.1, .3, .5, .7, .9}}]</
{{out}}
<pre>
Line 925:
=={{header|Nim}}==
{{trans|Go}}
<
const T = 100
Line 949:
let k = sum / (T * n)
echo &"{n:9} {k:15.4f} {k - theory:10.6f}"</
{{out}}
Line 989:
=={{header|Pascal}}==
{{trans|C}}{{works with|Free Pascal}}
<
{$MODE objFPC}//for using result,parameter runs becomes for variable..
uses
Line 1,041:
ip := ip+2;
end;
end.</
Output
<pre>running 1000 tests each:
Line 1,074:
=={{header|Perl}}==
{{trans|Raku}}
<
my ($n, $p) = @_;
my $r = join '',
Line 1,090:
printf " R(n, p)= %f\n", $r / t;
}
}</
{{out}}
<pre>t= 100
Line 1,116:
=={{header|Phix}}==
{{trans|zkl}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">run_test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">runs</span><span style="color: #0000FF;">)</span>
Line 1,149:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 1,182:
=={{header|Python}}==
<
from random import random
from math import fsum
Line 1,205:
sim = fsum(mean_run_density(n, p) for i in range(t)) / t
print('t=%3i p=%4.2f n=%5i p(1-p)=%5.3f sim=%5.3f delta=%3.1f%%'
% (t, p, n, limit, sim, abs(sim - limit) / limit * 100 if limit else sim * 100))</
{{out}}
Line 1,230:
=={{header|Racket}}==
<
(require racket/fixnum)
(define t (make-parameter 100))
Line 1,261:
(module+ test
(require rackunit)
(check-eq? (Rn (fxvector 1 1 0 0 0 1 0 1 1 1)) 3))</
{{out}}
<pre>
Line 1,298:
=={{header|REXX}}==
{{trans|Fortran}}
<
Numeric Digits 20
Call random(,12345) /* make the run reproducable */
Line 1,326:
Say format(n,10)' ' format(sim,2,4)' 'format(sim-theory,2,6)
End
End</
{{out}}
<pre>p: 0.1000 theory: 0.0900 t: 100
Line 1,365:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
say 't= ', constant t = 100;
Line 1,374:
printf " R(%6d, p)= %f\n", $n, t R/ [+] R($n, $p)/$n xx t
}
}</
{{out}}
<pre>t= 100
Line 1,400:
=={{header|Sidef}}==
{{trans|Raku}}
<
n.of { 1.rand < p ? 1 : 0}.sum;
}
Line 1,412:
printf (" R(n, p)= %f\n", t.of { R(n, p) }.sum/n / t);
}
}</
{{out}}
Line 1,440:
=={{header|Tcl}}==
<
for {set s ""} {[string length $s] < $length} {} {
append s [expr {rand() < $probability}]
Line 1,472:
}
puts ""
}</
{{out}}
<pre>
Line 1,499:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,536:
}
System.print()
}</
{{out}}
Line 1,572:
=={{header|zkl}}==
{{trans|C}}
<
cnt:=0; do(runs){
pv:=0; do(len){
Line 1,581:
}
return(cnt.toFloat() / runs / len);
}</
<
" p\t n\tK\tp(1-p)\t diff\n"
"-----------------------------------------------");
Line 1,594:
}
println();
}</
{{out}}
<pre>
|