Anonymous user
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 [
randomList n p = take n . map f <$> getRandomRs (0,1)
where f n = if (n > p) then
countRuns xs =
zipWith (\x y -> x .&. xor y 1) xs (tail xs ++ [0])
calcK :: Int -> Double -> Rand StdGen Double
Line 287 ⟶ 283:
<pre>./percolation
p= 0.1, K(p)= 0.090
n= 10, estimated K(p)= 0.
n= 100, estimated K(p)= 0.
n= 1000, estimated K(p)= 0.
n= 10000, estimated K(p)= 0.
n= 100000, estimated K(p)= 0.
p= 0.3, K(p)= 0.210
n= 10, estimated K(p)= 0.
n= 100, estimated K(p)= 0.
n= 1000, estimated K(p)= 0.
n= 10000, estimated K(p)= 0.
n= 100000, estimated K(p)= 0.
p= 0.5, K(p)= 0.250
n= 10, estimated K(p)= 0.
n= 100, estimated K(p)= 0.
n= 1000, estimated K(p)= 0.
n= 10000, estimated K(p)= 0.
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.
n= 1000, estimated K(p)= 0.
n= 10000, estimated K(p)= 0.
n= 100000, estimated K(p)= 0.210
p= 0.9, K(p)= 0.090
n= 10, estimated K(p)= 0.
n= 100, estimated K(p)= 0.
n= 1000, estimated K(p)= 0.
n= 10000, estimated K(p)= 0.
n= 100000, estimated K(p)= 0.
</pre>
|