Proposal: Generalize the RandomGen and Random classes

Henning Thielemann schlepptop at henning-thielemann.de
Sun Oct 10 05:53:20 EDT 2010


Simon Peyton-Jones schrieb:
> | > RandomGen and Random classes assume generators produce Int values.
> | > This is non-ideal as many high speed generators produce special values
> | > (ex: doubles) or generic values (bit streams / bytestrings) that can
> | > be converted directly to types easier than coercing to Int then to an
> | > 'a' via the Random class.	
> 
> On looking at this again, I think the proposed API looks over-elaborate.  Surely there must be a better way?
> 
> The current design (trimmed down) is like this:
>   class RandomGen g where
>     next :: g -> (Int, g)
> 
>   class Random a  where
>    randoms  :: forall g. RandomGen g => g -> [a]
> 
> The logic is
>  * RandomGen is a way to get a supply of bits, here expressed as Int
>    Maybe the class should have been called (BitSupply g)

Couldn't we just have

>   class RandomGen g where
>     next :: g -> (Bool, g)

where the generator can be internally based on Word32, Word64 or
whatever is appropriate? Then the Random methods compose the bits to
Ints, Integers, Word16, Float etc. but some optimizer rules take care of
that the bit representation is actually not needed in common cases?



More information about the Libraries mailing list