[SciPy-dev] Monte Carlo package

David M. Cooke cookedm at physics.mcmaster.ca
Tue Jan 24 14:44:13 CST 2006

Ed Schofield <schofield at ftw.at> writes:

> Robert Kern wrote:
>>Ed Schofield wrote:
>>>Hi all,
>>>The Monte Carlo package now builds and runs fine for me on MinGW.  I'd
>>>now like to ask for some help with testing on other platforms.  (Are
>>>there any that don't define the rand() function? ;)
>>I really would prefer that it not use rand() but use numpy.random instead. What
>>do you need to make this happen?
> Hmmm ... do you mean rk_random() defined in
> numpy/random/mtrand/randomkit.c? I guess I could link to this instead of
> the system default rand(), but this is probably overkill for this
> application.  I just need a single 30-bit random number per instance, to
> use as a seed.  I could use the system time directly, but at the moment
> I use rand() to scramble it up a little first:

If you just need a random seed, you don't need a C API to get it: you
could call Python (random.random(), for instance):

unsigned int
    PyObject *randomModule = NULL, *orandom = NULL, *oseed = NULL;
    unsigned int seed = 0;

    randomModule = PyImport_ImportModule("random");
    if (!randomModule) { goto fail; }

    orandom = PyObject_GetAttrString(randomModule, "random");
    if (!orandom) { goto fail; }

    oseed = PyObject_CallObject(orandom, NULL);
    if (!oseed) { goto fail; }

    seed = (unsigned int)PyInt_AsLong(oseed);

    return seed;

|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

More information about the Scipy-dev mailing list