Percolation/Mean run density: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(New post.) |
||
Line 744: | Line 744: | ||
500 0.90 4096 0.090 0.090 0.1 |
500 0.90 4096 0.090 0.090 0.1 |
||
500 0.90 16384 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; |
|||
final int threshold = (int) ( aProbability * RANDOM_MAXIMUM ); |
|||
for ( int run = 0; run < aRunCount; run++ ) { |
|||
int previousBit = 0; |
|||
int length = aLength; |
|||
while ( length-- > 0 ) { |
|||
int nextBit = ( RANDOM.nextInt(RANDOM_MAXIMUM + 1) < threshold ) ? 1 : 0; |
|||
if ( previousBit < nextBit ) { |
|||
count += 1.0; |
|||
} |
|||
previousBit = nextBit; |
|||
} |
|||
} |
|||
return count / aRunCount / aLength; |
|||
} |
|||
private static ThreadLocalRandom RANDOM = ThreadLocalRandom.current(); |
|||
private static final int RANDOM_MAXIMUM = Integer.MAX_VALUE - 1; |
|||
} |
|||
</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> |
</pre> |
||