Percolation/Mean run density: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 4 users not shown)
Line 42:
 
F runs(v)
R sum(zip(v, v[1..] [+] [0]).map((a, b) -> (a [&] (-)~b)))
 
F mean_run_density(n, p)
Line 79:
t=500 p=0.90 n= 4096 p(1-p)=0.090 sim=0.090 delta=0.0%
t=500 p=0.90 n=16384 p(1-p)=0.090 sim=0.090 delta=0.0%
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C}}
<syntaxhighlight lang="algol68">
BEGIN
 
# just generate 0s and 1s without storing them #
PROC run test = ( REAL p, INT len, runs )REAL:
BEGIN
INT count := 0;
REAL thresh = p;
TO runs DO
INT x := 0;
FOR i FROM len BY -1 TO 1 DO
INT y = ABS ( random < thresh );
count +:= ABS ( x < y );
x := y
OD
OD;
count / runs / len
END # run test # ;
 
print( ( "running 1000 tests each:", newline ) );
print( ( " p n K p(1-p) diff", newline ) );
print( ( "----------------------------------------------", newline ) );
FOR ip BY 2 TO 9 DO
REAL p = ip / 10;
REAL p1p = p * (1 - p);
INT n := 10;
WHILE ( n *:= 10 ) <= 100000 DO
REAL k = run test( p, n, 1000 );
print( ( fixed( p, -4, 1 ), whole( n, -9 ), fixed( k, -8, 4 )
, fixed( p1p, -8, 4 ), fixed( k - p1p, 9, 4 )
, " (", fixed( ( k - p1p ) / p1p * 100, 5, 2 ), "%)", newline
)
)
OD;
print( ( newline ) )
OD
END
</syntaxhighlight>
{{out}}
<pre>
running 1000 tests each:
p n K p(1-p) diff
----------------------------------------------
0.1 100 0.0898 0.0900 -0.0002 (-0.21%)
0.1 1000 0.0902 0.0900 +0.0002 (+0.20%)
0.1 10000 0.0900 0.0900 +0.0000 (+0.05%)
0.1 100000 0.0900 0.0900 +0.0000 (+0.04%)
 
0.3 100 0.2105 0.2100 +0.0005 (+0.22%)
0.3 1000 0.2098 0.2100 -0.0002 (-0.12%)
0.3 10000 0.2100 0.2100 +0.0000 (+0.02%)
0.3 100000 0.2101 0.2100 +0.0001 (+0.03%)
 
0.5 100 0.2536 0.2500 +0.0035 (+1.42%)
0.5 1000 0.2504 0.2500 +0.0004 (+0.16%)
0.5 10000 0.2501 0.2500 +0.0001 (+0.03%)
0.5 100000 0.2500 0.2500 +0.0000 (+0.01%)
 
0.7 100 0.2155 0.2100 +0.0055 (+2.60%)
0.7 1000 0.2107 0.2100 +0.0007 (+0.33%)
0.7 10000 0.2101 0.2100 +0.0001 (+0.06%)
0.7 100000 0.2100 0.2100 -0.0000 (-0.02%)
 
0.9 100 0.0982 0.0900 +0.0082 (+9.12%)
0.9 1000 0.0902 0.0900 +0.0002 (+0.27%)
0.9 10000 0.0901 0.0900 +0.0001 (+0.11%)
0.9 100000 0.0900 0.0900 +0.0000 (+0.01%)
</pre>
 
Line 280 ⟶ 351:
t=500, p=0.90, n= 4096, p(1-p)=0.09000, sim=0.09047, delta=0.5%
t=500, p=0.90, n=16384, p(1-p)=0.09000, sim=0.09007, delta=0.1%</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
numfmt 3 6
for p in [ 0.1 0.3 0.5 0.7 0.9 ]
theory = p * (1 - p)
print "p:" & p & " theory:" & theory
print " n sim"
for n in [ 1e2 1e3 1e4 ]
sum = 0
for t to 100
run = 0
for j to n
h = if randomf < p
if h = 1 and run = 0
sum += 1
.
run = h
.
.
print n & " " & sum / n / t
.
print ""
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 744 ⟶ 840:
500 0.90 4096 0.090 0.090 0.1
500 0.90 16384 0.090 0.090 0.1
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.concurrent.ThreadLocalRandom;
 
public final class PercolationMeanRun {
 
public static void main(String[] aArgs) {
System.out.println("Running 1000 tests each:" + System.lineSeparator());
System.out.println(" p\tlength\tresult\ttheory\t difference");
System.out.println("-".repeat(48));
for ( double probability = 0.1; probability <= 0.9; probability += 0.2 ) {
double theory = probability * ( 1.0 - probability );
int length = 100;
while ( length <= 100_000 ) {
double result = runTest(probability, length, 1_000);
System.out.println(String.format("%.1f\t%6d\t%.4f\t%.4f\t%+.4f (%+.2f%%)",
probability, length, result, theory, result - theory, ( result - theory ) / theory * 100));
length *= 10;
}
System.out.println();
}
 
}
private static double runTest(double aProbability, int aLength, int aRunCount) {
double count = 0.0;
for ( int run = 0; run < aRunCount; run++ ) {
int previousBit = 0;
int length = aLength;
while ( length-- > 0 ) {
int nextBit = ( random.nextDouble(1.0) < aProbability ) ? 1 : 0;
if ( previousBit < nextBit ) {
count += 1.0;
}
previousBit = nextBit;
}
}
return count / aRunCount / aLength;
}
 
private static ThreadLocalRandom random = ThreadLocalRandom.current();
 
}
</syntaxhighlight>
{{ out }}
<pre>
Running 1000 tests each:
 
p length result theory difference
------------------------------------------------
0.1 100 0.0899 0.0900 -0.0001 (-0.07%)
0.1 1000 0.0902 0.0900 +0.0002 (+0.18%)
0.1 10000 0.0900 0.0900 +0.0000 (+0.02%)
0.1 100000 0.0900 0.0900 -0.0000 (-0.00%)
 
0.3 100 0.2110 0.2100 +0.0010 (+0.47%)
0.3 1000 0.2101 0.2100 +0.0001 (+0.05%)
0.3 10000 0.2100 0.2100 -0.0000 (-0.01%)
0.3 100000 0.2100 0.2100 -0.0000 (-0.01%)
 
0.5 100 0.2516 0.2500 +0.0015 (+0.62%)
0.5 1000 0.2509 0.2500 +0.0009 (+0.37%)
0.5 10000 0.2499 0.2500 -0.0001 (-0.04%)
0.5 100000 0.2500 0.2500 +0.0000 (+0.00%)
 
0.7 100 0.2145 0.2100 +0.0045 (+2.12%)
0.7 1000 0.2106 0.2100 +0.0006 (+0.28%)
0.7 10000 0.2101 0.2100 +0.0001 (+0.06%)
0.7 100000 0.2100 0.2100 -0.0000 (-0.00%)
 
0.9 100 0.0970 0.0900 +0.0070 (+7.74%)
0.9 1000 0.0910 0.0900 +0.0010 (+1.15%)
0.9 10000 0.0901 0.0900 +0.0001 (+0.06%)
0.9 100000 0.0900 0.0900 +0.0000 (+0.00%)
</pre>
 
Line 1,499 ⟶ 1,673:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
9,485

edits