Home > OS >  Random char number in Haskell
Random char number in Haskell

Time:04-11

I want to generate random number in Haskell and to use this number for concatenation with my text. I found this function but it doesn't give me char type. How can I change this function?

rollDice :: IO Int 
rollDice = getStdRandom (randomR (1,3))

CodePudding user response:

randomR :: (Random a, RandomGen g) => (a, a) -> g -> (a, g) can generate random items for any type that is an instance of the Random typeclass, hence for a Char, you can work with:

rollDice :: IO Char
rollDice = getStdRandom (randomR ('a', 'z'))

or for any value of Char:

rollDice :: IO Char
rollDice = getStdRandom random
  • Related