[NCLUG] Python v.s. Ruby, and PRNGs

Chad Perrin perrin at apotheon.com
Thu Apr 10 00:19:23 MDT 2008


On Wed, Apr 09, 2008 at 07:02:16PM -0600, Stephen Warren wrote:
> 
> So, from memory last night's Ruby Chi^2 was like 10.x and Python's 14.x?
> As such, this isn't enough to imply that there's any meaningful
> difference between the PRNGs used by those two languages, based on a
> sample size of around 1-3 runs.

Considering that both Ruby and Python use the Mersenne Twister as PRNG
algorithm, it's not surprising that they end up with roughly equivalent
results for apparent randomness, particularly if you feed them both the
same seed.  My cursory search through Google seems to suggest that both
Python and Ruby even use the same period (2^19937 − 1, probably because
that's what the MIT19937 variant uses), so there's even less reason to
expect Ruby and Python to have substantively different apparent
randomness.

By point of reference (and yes, I tested this), here's a Python example:

  >>> random.seed(2004)
  >>> random.randrange(1,10)
  5

. . . and here's a Ruby example:

  irb(main):016:0> srand(2004);rand(10)
  => 5

You'll get the same result every time.

I haven't checked around what the default seed is for Python's REPL, but
Ruby's (called irb) uses the current time as reported by the host system.

Thus, the choice between Python and Ruby for this particular use should
boil down to nothing more than availability of the interpreter, personal
preference, and performance concerns.  My personal preference is Ruby,
both are available to me, and I don't see that for these purposes there's
any notable performance difference, so my choice should be obvious.  I
rather suspect that a few others' (in NCLUG) choice would be Python.

-- 
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Isaac Asimov: "Part of the inhumanity of the computer is that, once it is
completely programmed and working smoothly, it is completely honest."
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <http://lists.nclug.org/pipermail/nclug/attachments/20080410/c6614478/attachment.pgp>


More information about the NCLUG mailing list