Fergus Henderson wrote:
> 
> On 02-Feb-2000, Simon Peyton-Jones <[EMAIL PROTECTED]> wrote:
> > Guess what?  I have been driven to the conclusion that
> > the Haskell 98 Random library contains a fundamental, but
> > easily fixed flaw.  So I propose to declare a new 'typo'.
> > (Yes, I am all too aware of the danger of typo-creep.)
> >
> > The library is currently based on the RandomGen class,
> > whose signature is:
> >
> >       class RandomGen g wher
> >         next :: g  -> (Int, g)
> >         split :: g -> (g, g)
> >
> > The difficulty is that there is absolutely no robust way to
> > use such a generator to generate random numbers uniformly
> > distributed in a given range.
> 
> I think that is not true.
>
> > Why not?  Because there's no
> > clue as to the precise range of Ints produced by next.
> 
> You can figure out the range as you go along, by seeing what `next'
> returns.  This makes things a bit more complex that it would otherwise
> need to be, but it's still possible.  See the code below.

I like the clever code.  Unfortunately, it's not just more complex, it's
less efficient.  You will consume at least 3 random numbers from `next`
each time you need one new random number.  That cuts the RNG's period by
at least two thirds.  

I also think it could/would distort the statistical characteristics of
the RNG.  I came up with a different hack that would have worked great
if the RNG was perfect, but as it was, I found it really skewed the mean
of output numbers.  From that experience, I feel like your code would
have some similar effects.  For instance, initial ascending sequences
from `next` get dropped by getRands:

      [1,3,7,10,21,2] -> [2]

Same with descending sequences.  That may not seem bad, but getRands is
called each time the user program requests a new number (via randN or
randsN).  I think that means you'll see longer ascending or descending
sequences much less commonly from randN than you do from next.

You might understand these issues much better than I do; I'm not a
mathematician or statistician.  I just want to point out the problems I
see.

---- cool hack snipped ----

Thanks,
Matt

Reply via email to