[Haskell-beginners] Random Generator

David McBride toad3k at gmail.com
Wed Apr 1 15:32:48 UTC 2015


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)

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.



On Wed, Apr 1, 2015 at 11:08 AM, Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:

> Hi,
>
> I am trying to use the output value from the random function to generate
> the random generator for the next value in list.
>
> Below is the code -
>
> ----
> import System.Random
> myrandoms :: (RandomGen g, Random a) => g -> [a]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen (value::Int))
> ----
>
> however the compilation fails when the module is loaded -
>
> [1 of 1] Compiling Main             ( myrandoms.hs, interpreted )
>
> myrandoms.hs:3:80:
>     Could not deduce (a ~ Int)
>     from the context (RandomGen g, Random a)
>       bound by the type signature for
>                  myrandoms :: (RandomGen g, Random a) => g -> [a]
>       at myrandoms.hs:2:14-48
>       `a' is a rigid type variable bound by
>           the type signature for
>             myrandoms :: (RandomGen g, Random a) => g -> [a]
>           at myrandoms.hs:2:14
>     Relevant bindings include
>       value :: a (bound at myrandoms.hs:3:22)
>       myrandoms :: g -> [a] (bound at myrandoms.hs:3:1)
>     In the first argument of `mkStdGen', namely `(value :: Int)'
>     In the first argument of `myrandoms', namely
>       `(mkStdGen (value :: Int))'
>
> ----------
>
> Even though I am converting my 'value' parameter to Int in my new
> generator, I am unable to see the error behind this.
>
> Please can someone explain or even better provide a fix.
>
> Thanks,
> Shishir
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150401/df151122/attachment.html>


More information about the Beginners mailing list