Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
A way to generate random character strings for testing. Functions allow tuning of strings, setting the probability of alphabetic and upper-case characters in the resulting strings.
Hopefully this is useful for building test and benchmark cases that are
more meaningful than the overly random strings generated by libraries like
QuickCheck
.
Note: Please don't use this to generate passwords!
Examples:
Generate a 10-letter word with 1 in 10 upper-case characters
word <- randomWord' randomASCII (1%10) 10
Generate a list of 500 strings from the printable ISO-8859-1 characters with random lengths beween 5 and 20 characters.
let iso_printable = randomString $ onlyPrintable randomChar8 strings <- randomStringsLen iso_printable (5,20) 500 -- benchmarks ...
- randomChar :: IO Char
- randomASCII :: IO Char
- randomChar8 :: IO Char
- onlyWith :: (Char -> Bool) -> IO Char -> IO Char
- onlyPrintable :: IO Char -> IO Char
- onlyAlpha :: IO Char -> IO Char
- onlyAlpha' :: Rational -> IO Char -> IO Char
- onlyAlphaNum :: IO Char -> IO Char
- onlyUpper :: IO Char -> IO Char
- onlyLower :: IO Char -> IO Char
- randomClass :: Rational -> IO Char -> IO Char -> IO Char
- randomWord :: IO Char -> Int -> IO String
- randomWord' :: IO Char -> Rational -> Int -> IO String
- randomString :: IO Char -> Int -> IO String
- randomString' :: IO Char -> Rational -> Rational -> Int -> IO String
- randomStrings :: IO String -> Int -> IO [String]
- randomStringsLen :: (Int -> IO String) -> (Int, Int) -> Int -> IO [String]
Character generators
randomChar :: IO Char Source #
Generate a random Char
randomASCII :: IO Char Source #
Generate a random ASCII (7-bit) char in the printable range.
randomChar8 :: IO Char Source #
Generate a random ISO-8859-1 (8-bit) char
Specific character generators
:: (Char -> Bool) | predicate, like |
-> IO Char | random char generator, like |
-> IO Char |
Random character passing a test
:: Rational | range 0 to 1; chance of being an upper |
-> IO Char | random char generator; |
-> IO Char |
Generate a random alphabetic char with a probability of being upper-case.
:: Rational | range 0 to 1; chance of using the first generator |
-> IO Char | first generator; used if the random value is |
-> IO Char | second generator; used if the random value is |
-> IO Char |
Randomly generate one of two character types
String generators
:: IO Char | random char generator; |
-> Int | length |
-> IO String |
Generate a random string of alphabetic characters.
:: IO Char | random char generator; |
-> Rational | range 0 to 1; fraction of upper-case letters |
-> Int | length |
-> IO String |
Generate a random string
:: IO Char | random char generator; |
-> Rational | range 0 to 1; fraction of alphabetic characters |
-> Rational | range 0 to 1; fraction of upper-case letters |
-> Int | length |
-> IO String |
Generate a random string of printable characters with a balance of alphabetic and upper-case characters.
Sets of randomly generated strings
:: IO String | random string generator, eg. 'randomString randomAlpha 20' |
-> Int | list length |
-> IO [String] |
Generate a list of strings of uniform length.
randomStrings (randomString (onlyAlpha randomChar8) 20) 50
will build a list of 50 alphabetical strings, each 20 characters long.
:: (Int -> IO String) | random string generator, eg. |
-> (Int, Int) | range for string length |
-> Int | list length |
-> IO [String] |
Generate a list of strings of variable length.
Similar to randomStrings
, but generates strings with random length.
Example:
randomStringsLen (randomString' randomASCII (3%4) (1%8)) (10,30) 100
Returns a list of 100 strings that are between 10 and 30 characters, with 34 of them being alphabetical and 18 of those being upper-case.