module System.Random.Atmosphere (
getRandomNumbers
, getRandomSequence
, getRandomStrings
, getQuota
) where
import Data.Either
import Network.URI
import Network.HTTP.Simple
int_uri num min max
= concat [ "http://random.org/integers/?num=", show num
, "&min=", show min
, "&max=", show max
, "&col=1&base=10&format=plain&rnd=new" ]
seq_uri min max
= concat [ "http://www.random.org/sequences/?min=", show min
, "&max=", show max
, "&format=plain&rnd=new" ]
str_uri num len digits uppercase lowercase unique
= concat [ "http://www.random.org/strings/?num=", show num
, "&len=", show len
, "&digits=", if digits == True then "on" else "off"
, "&upperalpha=", if uppercase == True then "on" else "off"
, "&loweralpha=", if lowercase == True then "on" else "off"
, "&unique=", if unique == True then "on" else "off"
, "&format=plain&rnd=new" ]
getRandomNumbers :: Int
-> Int
-> Int
-> IO (Either String [Int])
getRandomNumbers num min max
| max <= min = return $ Left "err: minimum must be greater than maximum"
| num > 10000 = return $ Left "err: can't retrieve more than 10,000 numbers"
| abs min > 1000000000 = return $ Left "err: min can't be more/less than 1,000,000,000"
| abs max > 1000000000 = return $ Left "err: max can't be more/less than 1,000,000,000"
| otherwise = do let uri' = parseURI (int_uri num min max)
case uri' of
Nothing -> return $ Left "err: parseURI returned Nothing"
Just u -> retrieve u (map read . lines)
getRandomSequence :: Int
-> Int
-> IO (Either String [Int])
getRandomSequence min max
| max <= min = return $ Left "err: max must be greater than min"
| abs max > 1000000000 = return $ Left "err: max can't be greater/less than +/-1e9"
| abs min > 1000000000 = return $ Left "err: min can't be greater/less than +/-1e9"
| (max min)+1 > 10000 = return $ Left "err: sequence must be smaller than 10,000"
| otherwise = do let uri = parseURI (seq_uri min max)
case uri of
Nothing -> return $ Left "err: parseURI returned Nothing"
Just u -> retrieve u (map read . lines)
getRandomStrings :: Int
-> Int
-> Bool
-> Bool
-> Bool
-> Bool
-> IO (Either String [String])
getRandomStrings num len digits uppercase lowercase unique
| num > 10000 = return $ Left "err: maximum number of strings is 10,000"
| len > 20 = return $ Left "err: maximum str len is 20"
| otherwise = do let uri = parseURI (str_uri num len digits uppercase lowercase unique)
case uri of
Nothing -> return $ Left "err: parseURI returned Nothing"
Just u' -> retrieve u' lines
getQuota :: IO (Either String Int)
getQuota = do let uri = parseURI "http://random.org/quota/?format=plain"
case uri of
Nothing -> return $ Left "err: parseURI returned Nothing"
Just u -> retrieve u read
retrieve uri f = do s <- httpGet uri
case s of
Nothing -> return $ Left "err: httpGet returned nothing"
Just s' -> return $ Right (f s')