haskell

How can I generate a random sequence of elements from a list in Haskell?


I've had a look through https://hackage.haskell.org/package/random-1.1/docs/System-Random.html however I can't see how to use a custom "list" for example an alphanumeric list of ['a'..'z'] ++ ['0' .. '9']?

I suppose as a workaround I could instead map a random set of numbers instead.


Solution

  • The implementation of the work around I mentioned:

    Prelude> import System.Random
    Prelude System.Random> gen <- newStdGen 
    Prelude System.Random> x = ['a'..'z'] ++ ['0' .. '9']
    Prelude System.Random> fmap (x !! ) (take 10 $ randomRs (0, length x - 1 ) gen)
    "h4tm52rfox"