Skip to content

Instantly share code, notes, and snippets.

@sajith
Created February 25, 2016 13:32
Show Gist options
  • Save sajith/4e08ec7d8436655d8431 to your computer and use it in GitHub Desktop.
Save sajith/4e08ec7d8436655d8431 to your computer and use it in GitHub Desktop.
module RandomStr3 where
-- uses mwc-random for making random strings
import Data.List (nub)
import Data.Text as T (Text, pack)
import qualified Data.Vector.Unboxed as V (Vector, map, toList)
import Data.Word (Word8)
import System.Random.MWC (asGenST, uniformVector, withSystemRandom)
randomStr :: Int -> IO Text
randomStr len = do
v <- withSystemRandom . asGenST $ \gen -> uniformVector gen len
return $ T.pack $ V.toList $ V.map toChar (v :: V.Vector Word8)
toChar :: Enum a => a -> Char
toChar i = chars !! (c `mod` len)
where
c = fromEnum i
chars = ['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z']
len = length chars
allUnique :: IO Bool
allUnique = do
xs <- mapM (\_ -> randomStr 12) [1..1000]
return $ xs == nub xs
main :: IO ()
main = allUnique >>= print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment