[Numpy-discussion] random number facilities in numarray and main Python libs

Faheem Mitha faheem at email.unc.edu
Fri Oct 1 10:19:02 CDT 2004

On Fri, 1 Oct 2004, Bruce Southey wrote:

> Hi,
> I presume that you have R and can build the standalone library. I have
> attached my SWIG Smath.i , the SWIG Smath_wrap.c and the
> Smath.py files.  With these last two files, you shouldn't need SWIG.
> Note that I have not touched the void functions here as I have yet to check
> how these work in SWIG. Also, there are a few function in the R header that
> are only headers.  Eventually someone has to fixed these and add suitable
> documentation in some package.

I'm not sure what you mean by void functions.

> If you have SWIG you can directly use the Smath.i file - while SWIG can take
> a .h file directly it would not work in Python. So I just edited the header
> file into a .i file.
> The following is my process using Linux (I don't know about other platforms):
> 0) Have swig installed and built the R math library
> 1) $ swig -python Smath.i
> 2) $ gcc -c Smath_wrap.c -I/usr/local/include/python2.3
> -I/home/bsouthey/Rproject/R-1.9.1/src/nmath
> -I/home/bsouthey/Rproject/R-1.9.1/include
> 3) $ ld -shared Smath_wrap.o -o _Smath.so -lm -lRmath
> -L/home/bsouthey/Rproject/R-1.9.1/src/nmath/standalone
> Of course you must change the include (-I) and library (-L) paths to where
> python lives and standard alone Rmath library lives.

Thanks. I'm particularly interested in knowing how you interface with the 
random number generator at the top (Python) level. Can you supply an 

Specifically, I'm looking for the following method.

1) When C/C++ code called, reads seed from python random state.

2) Does its stuff.

3) Writes seed back to python level when it exits.

R has this built it, but here one needs to build ones own mechanism. This 
is complicated by the fact that Numarray and the base Python random 
library use different RNG mechanisms, so one has to chose which one to 
use. Which one did you use?


More information about the Numpy-discussion mailing list