Percolation/Mean run density: Difference between revisions

(Updated d entry)
Line 28:
* [http://mathworld.wolfram.com/s-Run.html s-Run] on Wolfram mathworld.
 
=={{header|C}}==
<lang c>#include <stdio.h>
#include <stdlib.h>
 
// just generate 0s and 1s without storing them
double run_test(double p, int len, int runs)
{
int r, x, y, i, cnt = 0, thresh = p * RAND_MAX;
 
for (r = 0; r < runs; r++)
for (x = 0, i = len; i--; x = y)
cnt += x < (y = rand() < thresh);
 
return (double)cnt / runs / len;
}
 
int main(void)
{
double p, p1p, K;
int ip, n;
 
puts( "running 1000 tests each:\n"
" p\t n\tK\tp(1-p)\t diff\n"
"-----------------------------------------------");
for (ip = 1; ip < 10; ip += 2) {
p = ip / 10., p1p = p * (1 - p);
 
for (n = 100; n <= 100000; n *= 10) {
K = run_test(p, n, 1000);
printf("%.1f\t%6d\t%.4f\t%.4f\t%+.4f (%+.2f%%)\n",
p, n, K, p1p, K - p1p, (K - p1p) / p1p * 100);
}
putchar('\n');
}
 
return 0;
}</lang>
{{out}}
<pre>
running 1000 tests each:
p n K p(1-p) diff
-----------------------------------------------
0.1 100 0.0900 0.0900 -0.0001 (-0.06%)
0.1 1000 0.0899 0.0900 -0.0001 (-0.11%)
0.1 10000 0.0902 0.0900 +0.0002 (+0.17%)
0.1 100000 0.0900 0.0900 -0.0000 (-0.03%)
 
0.3 100 0.2110 0.2100 +0.0010 (+0.46%)
0.3 1000 0.2104 0.2100 +0.0004 (+0.19%)
0.3 10000 0.2100 0.2100 -0.0000 (-0.02%)
0.3 100000 0.2100 0.2100 -0.0000 (-0.01%)
 
0.5 100 0.2516 0.2500 +0.0016 (+0.66%)
0.5 1000 0.2498 0.2500 -0.0002 (-0.10%)
0.5 10000 0.2500 0.2500 +0.0000 (+0.01%)
0.5 100000 0.2500 0.2500 +0.0000 (+0.01%)
 
0.7 100 0.2162 0.2100 +0.0062 (+2.93%)
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.07%)
0.9 1000 0.0905 0.0900 +0.0005 (+0.57%)
0.9 10000 0.0901 0.0900 +0.0001 (+0.09%)
0.9 100000 0.0900 0.0900 +0.0000 (+0.03%)
</pre>
=={{header|C++}}==
<lang C++>#include <algorithm>
Anonymous user