Jump to content

Percolation/Mean run density: Difference between revisions

insert header Pascal
(insert header Pascal)
Line 636:
0.9 10000 0.0899 0.00013
0.9 100000 0.0900 -0.0000144
</pre>
=={{header|Pascal}}==
{{trans|C}}{{works with|Free Pascal}}
<lang pascall>
{$MODE objFPC}//for using result,parameter runs becomes for variable..
uses
sysutils;//Format
const
MaxN = 100*1000;
 
function run_test(p:double;len,runs: NativeInt):double;
var
x, y, i,cnt : NativeInt;
Begin
result := 1/ (runs * len);
cnt := 0;
for runs := runs-1 downto 0 do
Begin
x := 0;
y := 0;
for i := len-1 downto 0 do
begin
x := y;
y := Ord(Random() < p);
cnt := cnt+ord(x < y);
end;
end;
result := result *cnt;
end;
 
//main
var
p, p1p, K : double;
ip, n : nativeInt;
Begin
randomize;
writeln( 'running 1000 tests each:'#13#10,
' p n K p(1-p) diff'#13#10,
'-----------------------------------------------');
ip:= 1;
while ip < 10 do
Begin
p := ip / 10;
p1p := p * (1 - p);
n := 100;
While n <= MaxN do
Begin
K := run_test(p, n, 1000);
writeln(Format('%4.1f %6d %6.4f %6.4f %7.4f (%5.2f %%)',
[p, n, K, p1p, K - p1p, (K - p1p) / p1p * 100]));
n := n*10;
end;
writeln;
ip := ip+2;
end;
end.</lang>
Output
<pre>running 1000 tests each:
p n K p(1-p) diff
-----------------------------------------------
0.1 100 0.0894 0.0900 -0.0006 (-0.70 %)
0.1 1000 0.0898 0.0900 -0.0002 (-0.17 %)
0.1 10000 0.0900 0.0900 0.0000 ( 0.02 %)
0.1 100000 0.0900 0.0900 0.0000 ( 0.04 %)
 
0.3 100 0.2112 0.2100 0.0012 ( 0.57 %)
0.3 1000 0.2101 0.2100 0.0001 ( 0.04 %)
0.3 10000 0.2099 0.2100 -0.0001 (-0.04 %)
0.3 100000 0.2099 0.2100 -0.0001 (-0.03 %)
 
0.5 100 0.2516 0.2500 0.0016 ( 0.66 %)
0.5 1000 0.2497 0.2500 -0.0003 (-0.14 %)
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.2144 0.2100 0.0044 ( 2.08 %)
0.7 1000 0.2107 0.2100 0.0007 ( 0.32 %)
0.7 10000 0.2101 0.2100 0.0001 ( 0.02 %)
0.7 100000 0.2100 0.2100 0.0000 ( 0.01 %)
 
0.9 100 0.0978 0.0900 0.0078 ( 8.69 %)
0.9 1000 0.0909 0.0900 0.0009 ( 0.96 %)
0.9 10000 0.0901 0.0900 0.0001 ( 0.10 %)
0.9 100000 0.0900 0.0900 0.0000 ( 0.02 %)
</pre>
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.