Percolation/Mean run density: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 31:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">UInt32 seed = 0
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))</langsyntaxhighlight>
 
{{out}}
Line 82:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 117:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 150:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <random>
#include <vector>
Line 205:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>t = 100
Line 237:
=={{header|D}}==
{{trans|python}}
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.random, std.math;
 
enum n = 100, p = 0.5, t = 500;
Line 259:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>t=500, p=0.10, n= 1024, p(1-p)=0.09000, sim=0.08949, delta=0.6%
Line 282:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; 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>
</lang>
{{out}}
<pre>
Line 332:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry io kernel math math.ranges math.statistics
random sequences ;
IN: rosetta-code.mean-run-density
Line 363:
: main ( -- ) header .1 .9 .2 <range> [ test ] each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 390:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="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>
</lang>
 
<pre>
Line 479:
=={{header|FreeBASIC}}==
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">Function run_test(p As Double, longitud As Integer, runs As Integer) As Double
Dim As Integer r, l, cont = 0
Dim As Integer v, pv
Line 513:
Print
Next ip
Sleep</langsyntaxhighlight>
<pre>Same as Phix, C, Kotlin, Wren, Pascal or zkl entry.</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 551:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 591:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Control.Monad.Random
import Control.Applicative
import Text.Printf
Line 619:
x <- newStdGen
forM_ [0.1,0.3,0.5,0.7,0.9] $ printKs x
</syntaxhighlight>
</lang>
<pre>./percolation
p= 0.1, K(p)= 0.090
Line 657:
The following works in both languages:
 
<langsyntaxhighlight lang="unicon">procedure main(A)
t := integer(A[2]) | 500
 
Line 671:
write(left(p,8)," ",left(n,8)," ",left(p*(1-p),10)," ",left(Ka/t, 10))
}
end</langsyntaxhighlight>
 
Output:
Line 697:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. translation of python
Line 719:
EMPTY
)
</syntaxhighlight>
</lang>
Session:
<pre>
Line 748:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">using Printf, Distributions, IterTools
 
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</langsyntaxhighlight>
 
{{out}}
Line 808:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import java.util.Random
Line 847:
println()
}
}</langsyntaxhighlight>
 
Sample output:
Line 881:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">meanRunDensity[p_, len_, trials_] :=
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}}]</langsyntaxhighlight>
{{out}}
<pre>
Line 925:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import random, strformat
 
const T = 100
Line 949:
 
let k = sum / (T * n)
echo &"{n:9} {k:15.4f} {k - theory:10.6f}"</langsyntaxhighlight>
 
{{out}}
Line 989:
=={{header|Pascal}}==
{{trans|C}}{{works with|Free Pascal}}
<langsyntaxhighlight lang="pascal">
{$MODE objFPC}//for using result,parameter runs becomes for variable..
uses
Line 1,041:
ip := ip+2;
end;
end.</langsyntaxhighlight>
Output
<pre>running 1000 tests each:
Line 1,074:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">sub R {
my ($n, $p) = @_;
my $r = join '',
Line 1,090:
printf " R(n, p)= %f\n", $r / t;
}
}</langsyntaxhighlight>
{{out}}
<pre>t= 100
Line 1,116:
=={{header|Phix}}==
{{trans|zkl}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,182:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from __future__ import division
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))</langsyntaxhighlight>
 
{{out}}
Line 1,230:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang 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))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,298:
=={{header|REXX}}==
{{trans|Fortran}}
<langsyntaxhighlight lang="rexx">/* REXX */
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</langsyntaxhighlight>
{{out}}
<pre>p: 0.1000 theory: 0.0900 t: 100
Line 1,365:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub R($n, $p) { [+] ((rand < $p) xx $n).squish }
 
say 't= ', constant t = 100;
Line 1,374:
printf " R(%6d, p)= %f\n", $n, t R/ [+] R($n, $p)/$n xx t
}
}</langsyntaxhighlight>
{{out}}
<pre>t= 100
Line 1,400:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func R(n,p) {
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);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,440:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc randomString {length probability} {
for {set s ""} {[string length $s] < $length} {} {
append s [expr {rand() < $probability}]
Line 1,472:
}
puts ""
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,499:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
 
Line 1,536:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,572:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">fcn run_test(p,len,runs){
cnt:=0; do(runs){
pv:=0; do(len){
Line 1,581:
}
return(cnt.toFloat() / runs / len);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("Running 1000 tests each:\n"
" p\t n\tK\tp(1-p)\t diff\n"
"-----------------------------------------------");
Line 1,594:
}
println();
}</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits