Skip to content

Instantly share code, notes, and snippets.

@sajith
Created February 25, 2016 12:22
Show Gist options
  • Save sajith/52261510343baaa5ed17 to your computer and use it in GitHub Desktop.
Save sajith/52261510343baaa5ed17 to your computer and use it in GitHub Desktop.
-- 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