Q:

How to solve "rigid type variable bound by" error for RandomGen?

I am trying to generate some random numbers and new random number generators. I didn't get too far yet, but I ran into this error and I don't understand how to fix it.
My code is:
getGenerator :: RandomGen g => g
getGenerator = snd (next (mkStdGen 42))

The error I get is:
Couldn't match expected type ‘g’ with actual type ‘StdGen’
  ‘g’ is a rigid type variable bound by
      the type signature

Can someone please explain what I am doing wrong? I also tried switching RandomGen to StdGen but get the error:
‘StdGen’ is applied to too many type arguments

I see that mkStdGen creates a StdGen, but according to the wiki page StdGen is an instance of RandomGen. Is there a way to somehow convert a StdGen to a RandomGen? I'm really confused right now.

A:

The problem is that your declaration is more general than the actual definition. Your declaration claims that the type of getGenerator could be any type that implements the RandomGen class. Suppose I had such a type, MyRandomGen. Then I should be able to write something like
let v = getGenerator :: MyRandomGen

and have v be bound to a MyRandomGen value.
However, your actual definition is not that general. The value of snd (next (mkStdGen 42)) is always of type StdGen, not an arbitrary instance g of RandomGen.
The solution is to be honest about what getGenerator evaluates to.
getGenerator :: StdGen
getGenerator = snd (next (mkStdGen 42))

