Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Created September 1, 2012 14:10
Show Gist options
  • Save dtchepak/3574228 to your computer and use it in GitHub Desktop.
Save dtchepak/3574228 to your computer and use it in GitHub Desktop.
replacing field contents with random strings
import Control.Monad.State
import System.Random
type Field = String
replaceField :: String -> Field -> Field
replaceField newContent field =
let fieldName = takeWhile (/=':') field
in fieldName ++ ": " ++ newContent
fieldUpdate :: RandomGen g => Field -> State g Field
fieldUpdate f = do
newContent <- randomString
return $ replaceField newContent f
updateAll :: RandomGen g => [Field] -> g -> [Field]
updateAll = evalState . mapM fieldUpdate
randomChar :: (RandomGen g) => State g Char
randomChar = state $ randomR ('a','z')
randomStringWithLength :: (RandomGen g) => Int -> State g String
randomStringWithLength i = replicateM i randomChar
randomString :: (RandomGen g) => State g String
randomString = state (randomR (1,10)) >>= randomStringWithLength
input :: [Field]
input = ["first: hello", "second: world", "third: !!!"]
-- ghci> updateAll input (mkStdGen 321)
-- ["first: ddoxhpps","second: th","third: lryt"]
-- ghci> updateAll input (mkStdGen 3212)
-- ["first: ct","second: pohjx","third: klryey"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment