Created
February 25, 2016 12:22
-
-
Save sajith/52261510343baaa5ed17 to your computer and use it in GitHub Desktop.
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
-- copied from Network.Mail.Mime | |
module RandomStr where | |
import Control.Arrow (first) | |
import Data.List (nub) | |
import System.Random | |
randomString :: RandomGen d => Int -> d -> (String, d) | |
randomString len = | |
first (map toChar) . sequence' (replicate len (randomR (0, 61))) | |
where | |
sequence' [] g = ([], g) | |
sequence' (f:fs) g = | |
let (f', g') = f g | |
(fs', g'') = sequence' fs g' | |
in (f' : fs', g'') | |
toChar i | |
| i < 26 = toEnum $ i + fromEnum 'A' | |
| i < 52 = toEnum $ i + fromEnum 'a' - 26 | |
| otherwise = toEnum $ i + fromEnum '0' - 52 | |
randomStr :: Int -> IO String | |
randomStr n = do | |
g <- newStdGen | |
let (s, _) = randomString n g | |
return s | |
allUnique :: IO Bool | |
allUnique = do | |
xs <- mapM (\_ -> randomStr 12) [1..10000] | |
return $ xs == nub xs | |
main :: IO () | |
main = allUnique >>= putStrLn . show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment