Elementary cellular automaton/Random number generator: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|Haskell}}: Minor reduction. Applied Ormolu.)
Line 221: Line 221:
Assume the comonadic solution given at [[Elementary cellular automaton#Haskell]] is packed in a module <code>CellularAutomata</code>
Assume the comonadic solution given at [[Elementary cellular automaton#Haskell]] is packed in a module <code>CellularAutomata</code>


<lang Haskell>import CellularAutomata (runCA, rule, fromList)
<lang Haskell>import CellularAutomata (fromList, rule, runCA)
import Data.List (unfoldr)
import Control.Comonad
import Control.Comonad
import Data.List (unfoldr)


rnd = fromBits <$> unfoldr (pure . splitAt 8) bits
rnd = fromBits <$> unfoldr (pure . splitAt 8) bits
where size = 80
where
size = 80
bits = extract <$> runCA (rule 30) (fromList (1:replicate size 0))
bits =
extract
<$> runCA
(rule 30)
(fromList (1 : replicate size 0))


fromBits = foldl (\res x -> 2*res + x) 0</lang>
fromBits = foldl ((+) . (2 *)) 0</lang>


{{Out}}
{{Out}}