Skip to content

Instantly share code, notes, and snippets.

@njsand
Created April 27, 2017 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njsand/94791a518c16818c31a5a13e303b3a00 to your computer and use it in GitHub Desktop.
Save njsand/94791a518c16818c31a5a13e303b3a00 to your computer and use it in GitHub Desktop.
Output random letters from [a-zA-Z] (haskell)
-- Output random letters from [a-zA-Z].
import System.Random
import System.Environment
import Control.Applicative
main :: IO ()
main = do
args <- getArgs
-- Default to 15 chars if no arg is supplied.
let length = case args of (n:_) -> read n
[] -> 15
randomString <- mixedpw length
putStrLn randomString
-- "If" as a function. Apparently this does not exist elsewhere!
if' :: Bool -> a -> a -> a
if' True x _ = x
if' False _ y = y
-- Random password N digits long from the set [a-zA-Z].
mixedpw :: Int -> IO String
mixedpw n = do
g <- newStdGen
g' <- newStdGen
g'' <- newStdGen
let lowers = take n $ randomRs ('a', 'z') g
uppers = take n $ randomRs ('A', 'Z') g'
mix = take n $ randoms g''
return $ zipWith3 if' mix lowers uppers
-- Lower case random password.
lowerpw :: Int -> IO String
lowerpw n = do
g <- newStdGen
return $ take n $ randomRs ('a', 'z') g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment