Monte Carlo Method and pi

Fernando Perez fperez528 at yahoo.com
Sun Jul 11 19:13:08 EDT 2004


Paul Rubin wrote:

> Fernando Perez <fperez528 at yahoo.com> writes:
>> Note that the above isn't 100% fair, since I'm calling the stdlib
>> rand, a C-coded simple generator, while v1 calls python's random.py,
>> a Python-coded Wichmann-Hill one.
> 
> random.random in python 2.2 and later calls the Mersenne Twister generator,
> a very fast generator implemented in C.

Sure?

[python]> ipython
Python 2.2.3 (#1, Oct 15 2003, 23:33:35)
Type "copyright", "credits" or "license" for more information.

IPython 0.6.1.cvs -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
@magic  -> Information about IPython's 'magic' @ functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import random

In [2]: random.random??
Type:           instance method
Base Class:     <type 'instance method'>
String Form:    <bound method Random.random of <random.Random instance at
0xa166834>>
Namespace:      Interactive
File:           /usr/lib/python2.2/random.py
Definition:     random.random(self)
Source:
def random(self):
        """Get the next random number in the range [0.0, 1.0)."""

        # Wichman-Hill random number generator.
        #
        # Wichmann, B. A. & Hill, I. D. (1982)
        # Algorithm AS 183:
        # An efficient and portable pseudo-random number generator
        # Applied Statistics 31 (1982) 188-190
        #
        # see also:
        #        Correction to Algorithm AS 183
        #        Applied Statistics 33 (1984) 123
        #
        #        McLeod, A. I. (1985)
        #        A remark on Algorithm AS 183
        #        Applied Statistics 34 (1985),198-200

        # This part is thread-unsafe:
        # BEGIN CRITICAL SECTION
        x, y, z = self._seed
        x = (171 * x) % 30269
        y = (172 * y) % 30307
        z = (170 * z) % 30323
        self._seed = x, y, z
        # END CRITICAL SECTION

        # Note:  on a platform using IEEE-754 double arithmetic, this can
        # never return 0.0 (asserted by Tim; proof too long for a comment).
        return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0

In [3]:

This looks strangely like python to me :)

You are correct under 2.3:

[python]> python2.3 `which ipython`
Python 2.3.3 (#1, Mar  6 2004, 22:39:33)
Type "copyright", "credits" or "license" for more information.

IPython 0.6.1.cvs -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
@magic  -> Information about IPython's 'magic' @ functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import random

In [2]: random.random??
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in method random of Random object at 0x907614c>
Namespace:      Interactive
Docstring:
    random() -> x in the interval [0, 1).


In [3]:

The fact that ipython fails to show source under 2.3 is an indication that the
method is loaded from a C extension.

But the tests I posted where done using 2.2.

Cheers,

f

ps.  My main point was to illustrate weave's usage, so this doesn't really
matter.



More information about the Python-list mailing list