From e2e345520ca8ffcaece19d27007e87dcfa02d864 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sat, 30 Jun 2018 10:14:47 -0700 Subject: Split `run` into a helper function and use a new random gen every time --- simspace/Main.hs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'simspace') diff --git a/simspace/Main.hs b/simspace/Main.hs index 07d2705..229bba4 100644 --- a/simspace/Main.hs +++ b/simspace/Main.hs @@ -2,7 +2,7 @@ module Main where import System.Environment import Control.Monad -import System.Random (getStdGen, randomRs) +import System.Random (newStdGen, randomRs) import Data.List.Split main :: IO () @@ -12,10 +12,17 @@ main = do [] -> putStrLn "Needs one argument" (n:_) -> run (read n :: Int) +-- | Run the cellular automata with a random seed. run :: Int -> IO () run n = do - g <- getStdGen + g <- newStdGen let init = take n $ randomRs (0,1) g + run' init + +-- | Version of 'run' that allows for entering your own initial seed. +run' :: [Int] -> IO () +run' init = do + let n = length init let zeros = take n $ repeat 0 let result = takeWhile' (/= zeros) $ chunksOf n $ compute n init forM_ result $ \r -> putStrLn $ show r -- cgit v1.2.3