Percolation/Mean run density: Difference between revisions

→‎{{header|Haskell}}: Changed countruns to use bitwise ops to calculate
(Added Haskell)
(→‎{{header|Haskell}}: Changed countruns to use bitwise ops to calculate)
Line 256:
import Text.Printf
import Control.Monad
import Data.Bits
 
data OneRun = OutRun | InRun deriving (Eq, Show)
 
randomList :: Int -> Double -> Rand StdGen [BoolInt]
randomList n p = take n . map f <$> getRandomRs (0,1)
where f n = if (n > p) then False0 else True1
 
countRuns xs = ffromIntegral . sum $ (0,OutRun)
zipWith (\x y -> x .&. xor y 1) xs (tail xs ++ [0])
where f (n,st) [] = if st == InRun then n+1 else n
f (n,st) (x:xs) = case (st,x) of
(OutRun, True) -> f (n,InRun) xs
(InRun, False) -> f (n+1,OutRun) xs
(InRun, True) -> f (n,st) xs
(OutRun, False) -> f (n,st) xs
 
calcK :: Int -> Double -> Rand StdGen Double
Line 287 ⟶ 283:
<pre>./percolation
p= 0.1, K(p)= 0.090
n= 10, estimated K(p)= 0.100000
n= 100, estimated K(p)= 0.090130
n= 1000, estimated K(p)= 0.088099
n= 10000, estimated K(p)= 0.094090
n= 100000, estimated K(p)= 0.090091
p= 0.3, K(p)= 0.210
n= 10, estimated K(p)= 0.300200
n= 100, estimated K(p)= 0.230250
n= 1000, estimated K(p)= 0.213209
n= 10000, estimated K(p)= 0.208209
n= 100000, estimated K(p)= 0.210211
p= 0.5, K(p)= 0.250
n= 10, estimated K(p)= 0.300200
n= 100, estimated K(p)= 0.280290
n= 1000, estimated K(p)= 0.265252
n= 10000, estimated K(p)= 0.249250
n= 100000, estimated K(p)= 0.250
p= 0.7, K(p)= 0.210
n= 10, estimated K(p)= 0.300
n= 100, estimated K(p)= 0.250200
n= 1000, estimated K(p)= 0.213210
n= 10000, estimated K(p)= 0.210209
n= 100000, estimated K(p)= 0.210
p= 0.9, K(p)= 0.090
n= 10, estimated K(p)= 0.300200
n= 100, estimated K(p)= 0.130090
n= 1000, estimated K(p)= 0.100089
n= 10000, estimated K(p)= 0.092095
n= 100000, estimated K(p)= 0.091090
</pre>
 
Anonymous user