[Haskell-cafe] System.Random.Mersenne - no randomR variants

Paul Moore p.f.moore at gmail.com
Mon Oct 5 16:48:20 EDT 2009


2009/10/5 Don Stewart <dons at galois.com>:
> p.f.moore:
>> I notice that System.Random.Mersenne has no randomR variants. Is there
>> a reason for this, or is it just an oversight? It makes porting code
>> from System.Random a little more complicated than it needs to be. I
>> know it's possible to write a function mapping a Double (or other
>> value, as appropriate) to a range, effectively replacing randomR, but
>> it's fiddly to get the edge cases right. A standard implementation
>> would be helpful.
>
> Basically, to use the type class interface defined in System.Random, you
> will suffer a 100x decrease in performance, as  the instances all
> convert via Integer.
>
> Rather than give people a surprise, I chose not to implement that
> interface.

Sounds fair enough :-)

Excuse my ignorance, but although System.Random uses Integer, why
can't your implementation work with Int and other fast types? Is this
something to do with how classes work?

Also, a minor documentation question - the documentation for
getStdRandom seems confused (or I am!):

getStdRandom :: (MTGen -> IO a) -> IO a	Source

Uses the supplied function to get a value from the current global
random generator, and updates the global generator with the new
generator returned by the function. For example, rollDice gets a
random integer between 1 and 6:

  rollDice :: IO Int
  rollDice = getMTRandom (randomR (1,6))

Is this right? It seems to imply there is a way to use randomR. If I
did, would performance be impacted as you mention above?

Thanks, and sorry for the dumb questions.

Paul.


More information about the Haskell-Cafe mailing list