[Haskell-beginners] Random Generator

Chaddaï Fouché chaddai.fouche at gmail.com
Wed Apr 1 21:04:05 UTC 2015


On Wed, Apr 1, 2015 at 5:32 PM, David McBride <toad3k at gmail.com> wrote:

> mkStdGen only accepts Ints as seeds.  But your random function, as you
> typed it, can return any type of random.  You either have to restrict your
> random function to returning ints, like so:
>
> myrandoms :: (RandomGen g) => g -> [Int]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen value)
>



*Is there any good reason we're not using newGen for its intended purpose
here and instead weakening our randomness, maybe extremely (imagine if a
list a Bool is asked for...) ???*


>
> Or you have to find a way to convert any Random a into an Int (not
> possible), or put another constraint on it, such that you can return all
> the types you might want that you have the ability to turn into ints, for
> example:
>
> myrandoms :: (RandomGen g, Random a, Intable a) => g -> [a]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen $ convertToInt value)
>
> class Intable a where
>   convertToInt :: a -> Int
>
> instance Intable Int where convertToInt = id
> instance Intable Integer where convertToInt = fromIntegral
> instance Intable Char where convertToInt s = undefined -- something
>
> Which is obviously tedious, but may be worthwhile depending on your
> application.
>
>
If this was really the way Shishir wanted to go, I would suggest simply
reusing the Enum typeclass rather than creating a new Intable typeclass,
since : fromEnum :: (Enum a) => a -> Int

Still a very bad and puzzling idea by the way...

-- 
Jedaï
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150401/acd49579/attachment-0001.html>


More information about the Beginners mailing list