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

Jim Hutchinson jim at ubuntu-rocks.org
Thu Apr 10 08:52:18 MDT 2008


On Thu, Apr 10, 2008 at 12:19 AM, Chad Perrin <perrin at apotheon.com> wrote:

> 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.
>

Interesting. I kind of wondered about the "how" of these. I guess an
extension of the project would be find two pRNGs that are actually
calculated using different algorithms. Basic would be fun.
-- 
Jim (Ubuntu geek extraordinaire)
----
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html



More information about the NCLUG mailing list