in reply to New CPAN Module: Math::Random::MT::Auto

I took a quick look at the code, looks pretty nice. My only real comment concerns the _seed function. You specify the different seeding strategies as code blocks with a huge if/elsif/elsif/else chain.

I would tend to split these blocks out into seperate subroutines, e.g. _seed_dev_random, _seed_random_org and so forth. And there's probably a bit of redundant code that could be hoisted out of the /dev/random and /dev/urandom seeding routines.

Also, bear in mind that that on OpenBSD, the two devices are known as /dev/srandom and /dev/random (If I remember correctly, it's been a few years...). FreeBSD also names them slightly differently.

If you adopt this approach, it simplifies the addition of additional seeding strategies with a minimum of fuss. It also opens up the possibility that client code can add their own seeding routine, maybe reading from /dev/audio, or running a one-way hash digest on the gzipped output of top(1) or something or other.

This is probably a better tactic for getting patches sent in for additional seeding routines. Telling people to fill out and array and pass it to the seeds() routine seems a little cumbersome to me. And if the seeds() routine is called without any parameters, you should return the current seed array, so that people have the chance of serialising the state of the generator, in case they wish to "pick up where they left off" in a subsequent program invocation.

Oh, one last thing... you ought to add some statistical tests in your test suite to test that there aren't any glaring non-random errors occurring. Just saying things are between 0 and 1 is a little too simple.

One thing I really liked about the original MT implementation when I looked at it was that you could get a random number back from 0 .. MAXINT. Which is really nice, because then it becomes trivial to generate numbers on the [0,1] interval, rather than [0,1), which is sometimes nice to have. And if you're scaling up to integers anyway, you can save a few cycles by avoiding the initial reciprocal division that way. Being able to get at the raw U32 value would be nice.

- another intruder with the mooring in the heart of the Perl

  • Comment on Re: New CPAN Module: Math::Random::MT::Auto

Replies are listed 'Best First'.
Re^2: New CPAN Module: Math::Random::MT::Auto
by jdhedden (Deacon) on Jun 22, 2005 at 20:57 UTC
    I would tend to split these blocks out into seperate subroutines...
    I'll do that.
    ... on OpenBSD, the two devices are known as /dev/srandom and /dev/random.... FreeBSD also names them slightly differently.
    I'll investigate this.
    ... opens up the possibility that client code can add their own seeding routine...
    Good idea. I'll add that in.
    And if the seeds() routine is called without any parameters, you should return the current seed array.
    I did make the seed array visible, but this is a better idea.
    ... so that people have the chance of serialising the state of the generator, in case they wish to "pick up where they left off" in a subsequent program invocation.
    The seed and the PRNG's state are different entities. However, I get the idea and I'll add a state() function for this purpose.
    ... you ought to add some statistical tests in your test suite...
    The statistical analysis of the Mersenne Twister is available on the web. The t/mersenne.t test checks the PRGN's workings by checking its output for a known seed. If that test file passes, then the PRGN is working properly. That should cover things.
    ... get a random number back from 0 .. MAXINT.... you can save a few cycles by avoiding the initial reciprocal division.... Being able to get at the raw U32 value would be nice.
    The rand32 function does return 0 .. 2^32-1 (i.e., the raw U32 value). I put it in specifically to avoid the floating point math when it's not needed.

    Thanks for the great feedback.