[Haskell-cafe] [ANN] random-stream package

Gökhan San gsan at stillpsycho.net
Thu Mar 19 19:48:47 EDT 2009


Manlio Perillo <manlio_perillo at libero.it> writes:

> Gökhan San ha scritto:
>> Manlio Perillo <manlio_perillo at libero.it> writes:
>>
>>> The stream generator implements tha RandomGen interface.
>>
>> This is really cool, though I think 'split' is a must. Maybe all
>> instances could share the same stream in the background, then it
>> wouldn't cause resource issues.
>>
>
> I have thought about this, but is it safe?
> I feared that this would break referential transparency.

You're absolutely right, my bad.

To test locally, I wrote a split function by merging Stream with StdGen,
but I'm not sure if it makes much sense statistically:

> data Stream = Stream StdGen L.ByteString

> instance RandomGen Stream where
>     next (Stream t s) = (xor it (fromIntegral is), Stream t' s')
>         where (it, t') = next t
>               (is, s', _) = (runGetState getWordhost) s 0
>     split (Stream t s) = let (left, right) = split t
>                          in (Stream left s, Stream right s)

I'd love to test how it scales if you happen to implement split on the
stream.

>> Also, IMHO mkStream should produce an IO Stream (like getStdGen), as
>> current implementation isn't referentially transparent; let the library
>> user decide whether to use unsafePerformIO.
>>
>
> The basic idea is that there is this system wide random number
> generator, that is always available.
> That's the reason why mkIOStream is hidden.

Ah, OK, I get it now.

-- 

Gökhan San


More information about the Haskell-Cafe mailing list