Created
April 27, 2017 06:58
-
-
Save njsand/94791a518c16818c31a5a13e303b3a00 to your computer and use it in GitHub Desktop.
Output random letters from [a-zA-Z] (haskell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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