[SciPy-dev] Monte Carlo package

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


cookedm at physics.mcmaster.ca (David M. Cooke) writes:
>
> 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
> random_seed(void)
> {
>     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);
>
> fail:
>     Py_XDECREF(oseed);
>     Py_XDECREF(orandom);
>     Py_XDECREF(randomModule);
>     return seed;
> }

Or something like that (random.random() returns a Python float, not a
long :)

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca




More information about the Scipy-dev mailing list